Best way to maintain lists of every object in every trigger via javascript

Hello and thanks for your time.

I’m in need of knowing if triggers have a certain object in them. I’m finding that on trigger enter and exit don’t always seem to fire in my game so I thought that I could use “on trigger stay” to find out if there is a certain thing in each trigger.

The set up is this; my entire level is covered with triggers, each with a unique numbered int and a camera target attached. My player (the “TriggerCamCol”) moves through the level and as they go along the camera moves with them to show what they need to see next. If they player dies (a small bubble pops) it calls to the camera to find and look at the triggercam target that is furthest along in the sequence that has an “awake” bubble in it.

I do not destroy on instantiate anything if a bubble dies or combines with another. The excess bubbles are moved off to a far away point.

Here is the script (same script on all of them) on the triggercams (the things that say the cam should move when a bubble enters them and also sets the condition of awakeBubInTrigger so the on-pop knows what to look at:

var bubInTrigger : GameObject;
var scriptOfBubInTrigger : Bubble;
var awakeBub : boolean;

awakeBubInTrigger = false;
  
function OnTriggerStay(other : Collider)
{	
	if (other.gameObject.tag == "TriggerCamCol") 
	{
		//get the parent gameobject, the bubble
		bubInTrigger = other.transform.parent.gameObject; 
		
		//get the script attached to the parent bubble
		scriptOfBubInTrigger = bubInTrigger.GetComponent(Bubble);
		
		//set the condition the cam script needs for on pop		
		if (scriptOfBubInTrigger.awake == true) 
		{
			awakeBubInTrigger = true;
		}	
		
		//check to see if the cam should move to look

                //if they are at start they were moved there by a spawn and we don't want to auto look on trigger
		if (scriptOfBubInTrigger.movedFromStart == false){return;}

                //if camera isn't looking at the camfocus it should
		if (camGame.targToFrame != camFocus.transform.position)
			{	
				camGame.FrameTargetNew(camFocus.transform.position);
				Debug.Log("Cam is moving to look at# : " + trigValue);
			}
		}

And here’s the only way I know of of clearing the needed bool, same script. Problem is is that sometimes when lots of things are moving around alot like when bubbles are combining and being popped, the bool doesn’t seem to be set to false.

function OnTriggerExit (other : Collider)
{
	if(other.gameObject.tag == "TriggerCamCol")
	{	
		awakeBubInTrigger = false;		
	}
}

Here’s the script in the camera that’s called once a small bubble pops to find what the camera should look at (without something setting off a trigger):

var trigTargToFocusOn : GameObject;
var bestTrigNum : int = 0;
var highestBubVal : int = 0;

///////////////////////////////////////
function FindBestTrigerCam()
{
	var triggerCamArray : GameObject[];
	var triggerCamScript : TriggerCamScript;
	
	yield WaitForSeconds(1.5); //give the bubs a moment to get in position
	
	//find all the triggerCam objects
	triggerCamArray = GameObject.FindGameObjectsWithTag ("TriggerCam"); 
	
	for (var trigger : GameObject in triggerCamArray)	
	{
		triggerCamScript = trigger.GetComponent(TriggerCamScript);
		print("trigNumber: " + triggerCamScript.trigValue + " AWAKE BUB IN TRIGGER: " + triggerCamScript.awakeBubInTrigger);
	
		if(triggerCamScript.trigValue >= bestTrigNum && triggerCamScript.awakeBubInTrigger == true) 
			{
				bestTrigNum = triggerCamScript.trigValue;
				trigTargToFocusOn = triggerCamScript.camFocus;
			}	
	}
	
//	Debug.Log("Best to look at ONPOP is# " + bestTrigNum);

	frameTarget(trigTargToFocusOn.transform.position);
	
	//below targToFrame for when the cam zooms back in
	targToFrame = trigTargToFocusOn.transform.position;
	
	bestTrigNum = 1;
	highestBubVal = 1;
}

I hope this isn’t to garbled or spaghetti to understand the intention. :slight_smile: Thanks for all the help guys.

You can set a boolean variable if the object has the type you want:

  var gotCamCol:boolean = false;

  function OnTriggerStay(other : Collider){

    if (other.gameObject.tag == "TriggerCamCol"){
      gotCamCol = true;
    }
  }

Whenever some object tagged TriggerCamCol enters the trigger, its gotCamCol variable is set to true - but it will not be reset when the object leaves the trigger. It’s useful to show if some of these objects ever entered the trigger, but if you want it to be true only when one or more objects of this type are in the trigger, it will not be enough. You can achieve this by resetting gotCamCol right after reading it - if there’s still an object of the correct type in the trigger, it will set gotCamCol again.

I think the easiest way to do this would be to just keep track of howmany of objectX enter and how many exit.

var objectCount : int = 0;
function OnTriggerEnter( enter : Collider){
    if( enter == whatEverYouWantToCount){
        objectCount++;
    }
}
function OnTriggerExit( exit : Collider){
    if( exit == whatEverYouWantToCount ){
        objectCount--;
    }
}

If the count is higher then zero you know at least one objectX is in your trigger. The reason I don’t use a Boolean is because if two enter (setting it to true twice) and one exits it will be set to false. If you only have one objectX in your scene at a time you might as well use booleans though.

You could of course maintain an actual list of what is in, but I don’t think that that would be a practical solution in your case. If what I suggested also doesn’t work for you I’d advice using Physics.OverlapSphere() to see if your object with a foo is within whatever range of object bar. This will only work for spherical ‘triggers’ though.

Turns out the script i posted (with a few edits not related to functionality) in my revised question did the trick.