The below script checks to see if 7 uniquely tagged objects have entered a collider.
If all 7 are inside, it will acknowledge with a message. Conversely, if any object leaves
the collider, a message will be produced. However, (and this is my question) if I delete
an object within the collider it will not be recognized as missing as it doesn’t OnTriggerExit.
•Is there a way that I can delete an object in this array and have it register as having exited?
Thanks for your time,
Greg
var allIn : boolean[]=new boolean[7];
//var allIn : boolean[]=new boolean[3];
function OnTriggerEnter (other:Collider){
// Check the Object tag and set the corresponding bool. Returns early if none
if(other.tag=="OBJECT1") allIn[0]=true;
else if(other.tag=="OBJECT2") allIn[1]=true;
else if(other.tag=="OBJECT3") allIn[2]=true;
else if(other.tag=="OBJECT4") allIn[3]=true;
else if(other.tag=="OBJECT5") allIn[4]=true;
else if(other.tag=="OBJECT6") allIn[5]=true;
else if(other.tag=="OBJECT7") allIn[6]=true;
else return;
// Check array is all false
var doIt=true;
for(i=0; i<7; i++)
if(! allIn[i])
doIt=false;
// Do something if we have all 7 inside
if(doIt)
{
Debug.Log("GOT ALL OBJECTS");
}
}
function OnTriggerExit (other:Collider){
//Check Object tags and unflag leaving objects
if(other.tag=="OBJECT1") allIn[0]=false;
else if(other.tag=="OBJECT2") allIn[1]=false;
else if(other.tag=="OBJECT3") allIn[2]=false;
else if(other.tag=="OBJECT4") allIn[3]=false;
else if(other.tag=="OBJECT5") allIn[4]=false;
else if(other.tag=="OBJECT6") allIn[5]=false;
else if(other.tag=="OBJECT7") allIn[6]=false;
else return;
var doIt=false;
for(i=0; i<7; i++)
if(! allIn[i])
doIt=true;
// Do something if we DON'T have all 7 inside
if(doIt)
{
//Destroy (gameObject);
Debug.Log("MISSING OBJECTS");
}
}
You can use flags to do it. I dont know alot of js but heres what it would look like in c#
Collider[] objects = new Collider[7];
int colliderFlags = 0;
static int IndexForTag( string tag ) {
case "OBJECT1": return 0;
case "OBJECT2": return 1;
case "OBJECT3": return 2;
case "OBJECT4": return 3;
case "OBJECT5": return 4;
case "OBJECT6": return 5;
case "OBJECT7": return 6;
default: return -1;
}
void OnTriggerEnter( Collider other ) {
int index = IndexForTag( other.tag );
if( index != -1 ) objects[index] = other;
}
void OnTriggerExit( Collider other ) {
int index = IndexForTag( other.tag );
if( index != -1 objects[index] == other ) objects[index] = null;
}
void Update() {
int newColliderFlags = 0;
int allFlags = 0;
for( int i = 0; i < objects.Length; i ++) {
int flag = 1 << i;
if( objects[i] ) newColliderFlags |= flag;
allFlags |= flag;
}
if( colliderFlags != newColliderFlags ) { // check for changes
bool wasAllThere = colliderFlags == allFlags;
bool newAllThere = newColliderFlags == allFlags;
if( wasAllThere!= newAllThere )// see if theres a change in all being there
{
if( newAllThere )
Debug.Log("GOT ALL OBJECTS");
else
Debug.Log("MISSING OBJECTS");
}
// apply the change
colliderFlags = newColliderFlags;
}
}
So what this does is it keeps track of each collider with tag in a array. Then on Update it checks the arrays for null values and differences.
But to answer your question on if you can get the physics trigger exit to be called like people should assume it should be no. This is just a work around.
Assuming your code is on a game object called “TheBigCollider”, you can just manually call TheBigCollider.OnTriggerExit(theObjectBeingDestroyed) right before you destroy the object in your code. Or you could add an OnDestroy() method to the seven gameobjects that manually calls TheBigCollider.OnTriggerExit(this)