- Home /
Destroyed gameobject become Null ?
Hi, I have an issue, I destroyed my gameObject "ColToMove" but "if (ColToMove!=null)" is still true. I also have an error console saying that "ColToMove.transform.position" has been destroyed and that I should check if it is not null or not destroy it.
This function ReplaceCube() is called in a OnCollisionExit2D().
Is someone can explain me why "if (ColToMove!=null)" is true ?
thanks
function ReplaceCube(col:Collision2D) {
RandomHightDown=Random.Range(1,3);
if (col.gameObject.tag != "StartCube") {
if (ColToMove!=null) {
if (RandomHightDown==1) {
ColToMove.transform.position = PreviousPosition + CubeEspaceHight;
PreviousPosition += CubeEspaceHight;
}
else if (RandomHightDown==2){
ColToMove.transform.position = PreviousPosition + CubeEspaceDown;
PreviousPosition += CubeEspaceDown;
}
}
ColToMove=col;
}
}
Answer by fafase · Dec 14, 2013 at 11:20 AM
If you destroy and call in the same frame, Destroy happens at the end of the frame, soyour object still exist until the end of the frame.
You could use DestroyImmediate().
... or just set it explicitly to null after your call to Destroy()
... that is just setting it null in the script calling it.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Why am i getting a null reference? 1 Answer
Array containing destroyed object = Null? 1 Answer
Can't destroy objects 1 Answer
missing gameobject ≠ Null? 1 Answer