- Home /
Deleted object fires OnTriggerEnter2D
I'm getting a OnTriggerEnter2D event with and object that was just deleted.
I'm using the following code to recycle my enemies:
Destroy(Chunk[0]);
int max = Chunks-1;
float px = FirstX;
for(int i=0;i<max;i++){
Chunk[i] = Chunk[i+1];
tChunk[i] = tChunk[i+1];
tChunk[i].localPosition = new Vector3(px,0,0);
px += ChunkWidth;
}
Chunk[max] = (GameObject) Instantiate(Prefab[Current]);
tChunk[max] = Chunk[max].transform;
tChunk[max].parent = tRoot;
tChunk[max].localPosition = new Vector3(px,0,0);
Chunk is an array of GameObjects while tChunk contains its respective transforms. Enemies (colliding objects) are contained inside the Prefabs I'm instantiating. So when I delete the first chunk all child objects are supposed to be deleted too... and they seem to be, since they disappear from the Hierarchy window... but oddly enought, sometimes I get an OnTriggerEnter2D event from the object that was just deleted.
To make sure I inserted some Debug.Log's in my code and what I get on the console is this:
BackgroundScroll called:
Destroy Enemy1 at: X=-6.57
OnTriggerEnter2D called:
Crash with Enemy1 at: X=-1.57
Since I'm making sure to have only one enemy of each type is on screen at any given time, the previous log referes to the same object.
Now the value of ChunkWidth is 5... that's why I suspect of the previous code, but I cannot understand why Chunk[0] the deleted one is changing position, and thus fall in the collision range, and even generate a collision event when it no longer exists.
Any Ideas?
Answer by georgeq · Nov 01, 2014 at 10:26 PM
SOLVED
just added:
tChunk[0].parent = null;
Before deleting the object...
It seems like Unity treats anything you do in your script as an atomic operation... so the general efect of deleting an object first or last is exactly the same, it also means anything you do to the parent object afects children, deleted or not. So, if you delete a child object and then move the parent object, the transform of the deleted child object is also affected...
It also seems like Unity doesn't realize the object is deleted immediatly, so if while changing its position it happens to collide with something, a collision is triggered, even when you had taken care to delete the object first!!!
Actually, you're closer than you think by writing "Unity doesn't realize the object is deleted immediatly". Unity doesn't actually delete a gameobject until one of those conditions are met : - Loading of a new level or reload of the current. - Instantiate or replace the deleted gameobject with a new one. - Lack of memory. If Unity temp files take too much of a burden, it might dump some of the backup asset... hence the "deleted gameobject".
This is a safeguard so that, in case of compilation error when updating at each frame, the engine doesn't miss anything. You could see this as whenever you delete something on a HDD. It's unaccessible, but can still be recovered as long as it doesn't have been replaced by new data (which can be empty (0) data).
You could see this as a family situation : a child might forget its parents, but a parent never forget its child. ;)
If you really want a game object to be destroyed, you have 2 possibilities : 1) What you did which is to force a "null" to replace the destroyed gameobject. (After all, "null" is something... like a "0" in the code) 2) You create a default empty filler that is always in the memory and whenever a gameobject gets destroyed, you replace it by this default empty filler. This can be touchy as it also means that the amount of gameobjects in a scene can skyreach so you need to manage it in a way the root (top parent) can get destroyed at some points.