- Home /
The question is answered, right answer was accepted
Destroying One Object Destroys Another?
Hi, all!
I have two cubes, and when they occupy the same space in 2D, an event is triggered that mixes their colors together, assigns the color to the first cube, and destroys the second cube.
However, when I destroy the second cube, the first goes with it, and vice versa!
I am confused as to why this is happening.
I can say that they are carrying the same scripts, and each contain instances of a class called CubeData that is on the script ColorCube, which is also a member of each cube. They also share the same parent.
What's up?
Thanks! - YA
Answer by IvovdMarel · Jun 19, 2014 at 10:16 PM
Where's the destroy-method called? Did you Debug.Log it to check if it was maybe called twice? (e.g. in both blocks).
The error is in your code, this is no standard behaviour
The Destroy method is called on a parent object that carries out the 'fusion' function. It takes two parameters- The first cube and the second cube. The function works changes on the first cube, and then explicitly deletes only the gameObject that is passed as a parameter in the second slot (The second cube)...
I Figured it out. Both cubes are calling the fusion function simultaneously, listing themselves as the first parameter and the other as the second, effectively destroying one another.
That only took me several hours to figure out -_-
Any ideas for how I can get just one to call the function?
In your fusion function check if both gameObjects exist. If one has already been destroyed then do nothing.
To add to this you could set the return type of your fusion function to bool, then return true if the fusion is successful and false if it fails.
I would also suggest not asking further questions on a answered thread. You will get more response by opening a new thread.