- Home /
How would I clear a "GameObject" variable?
I have:
var ninjaFighter : GameObject; var ninjaTosser : GameObject; private var ninja : GameObject;
if(!ninja) ninja = Instantiate(ninjaFighter,transform.position,transform.rotation);
The ninja is set to delete after a predetermined duration yet the "if(!ninja)" is still finding information in the "ninja" variable. How would I clear that variable so "if(!ninja)" will fire after the ninja has been destroyed?
Answer by Jessy · Apr 13, 2011 at 02:50 AM
What you're asking about is making the variable null. If the ninjaFighter object is really destroyed, then there is no way ninja can be anything but null, unless you assign something else to it, somewhere. You can always manually do
ninja = null;
but you should not. You should fix the code so that you're actually destroying the ninjaFighter.
Afterwards, you should figure out a way to do what you're doing, that doesn't necessitate constantly checking, like that, but rather, fires the Instantiate when appropriate. At least, I assume you're doing that. If you're not actually running that code in Update, or another function that is actually going to run continuously, then that's you're problem. The code you have above would only run once.
Thanks for the perfect answer! Just an FYI, the code in my original question was just snippets from a file and is not constantly running such as in an Update() function so no worries there. ;)
@Nico 2 - you should mark this answer as 'accepted' and close it!
Your answer
Follow this Question
Related Questions
Accessing itween variables 0 Answers
how do you delete variables or arrays? 3 Answers
lookAt on Y axis only 1 Answer
Speedometer Error. PLS help ((( 0 Answers