If two objects’ respective collision volumes are touching when one object is destroyed, will OnCollisionExit()/OnTriggerExit() (depending on the collider) be called on the remaining object? The situation I’m working on is a pipe game (similar to the old “Pipe Dreams” game or the hacking minigame in Bioshock), and if a pipe is removed from the board, I’m expecting that the following is called on the remaining objects that touch its endpoints (when a GameObject enters the collision volume, it is pushed onto connectedPipes.
):
var connectedPipes = new Array();
//...
function OnTriggerExit(other : Collider) {
var exitingPipe = other.gameObject;
if (exitingPipe.tag == "Pipe") {
for (var i = 0; i < connectedPipes.length; i++) {
if (connectedPipes[i] == exitingPipe) {
connectedPipes.splice(i, 1);
return;
}
}
}
}
To see if the puzzle is solved, each pipe on the board checks to see if it either touches the finish or touches a pipe that does:
var touchesFinish = false;
//...
function Update() {
touchesFinish = SearchForFinish();
}
//...
function SearchForFinish() {
for (var pipe : GameObject in connectedPipes) {
if (pipe) {
if (pipe.name == "Finish" || pipe.GetComponent(PipeScript).touchesFinish == true) {
return true;
}
}
}
return false;
}
But Unity reports the following:
The line it points to is “if (pipe)”, which is, if I understood the docs correctly, the way to check if an object is null.
So I suppose I have two questions:
- what should I be doing in this situation to avoid the missing reference exceptions?
- is there a more elegant way to handle business surrounding object destruction? AFAIK, there isn’t an OnDestroy() method in which I could tell the object to remove itself from these arrays before it’s destroyed.