- Home /
Issue in gameObject.SetActiveRecursively
Hi,
I am enabling a series of game objects with gameObject.SetActiveRecursively(true);
its something like :
void Activate()
{
thisGameObject.SetActiveRecursively(true);
totalCoins.DisplayTotalCoins = GlobalClass.TotalCoins.ToString();
}
here totalCoins is a component of a child of thisGameObject. I get a null reference when i try to acces totalCoins.Text
Plss help
Thanks in advance
its states that instance of totalCoins is not found
NullReferenceException: Object reference not set to an instance of an object
Answer by Michael Covert · Sep 19, 2012 at 05:22 PM
It's telling you what's wrong - "NullReferenceException: Object reference not set to an instance of an object". This doesn't mean your thing is inactive; it means you've never set it, or you've destroyed it. You'll need to make a public variable and assign it via the inspector, or use some code to find the particular object you're looking for.
If you do it via code, GameObject.Find() can get you the object, and calling get component on the result can get you your particular script.
If you're already trying to do this, note that GameObject.Find() only finds active objects - so you'll need to call it after turning the object on.
yeah, thanks a ton.... assigning it to a public variable and activating it worked......
Answer by john-essy · Sep 19, 2012 at 11:08 AM
ah i think i know where your problem is it should be this.gameObject.SetActiveRecursively(true)
Do you have a reference to that object because it doesnt seem like thier is a reference to the coins.
yeah, I am calling that object from outside this function, which works just fine.
I am not sure of this but does gameobject.SetRecursivelyActive()takes more than a frame to complete?
It may take longer than a frame i am not sure. But i have never had this problem before. and you do have it like this.blah blah and with a lower case g
Answer by shaico · Sep 19, 2012 at 04:18 PM
maybe you get null reference because you disable this object then try to call then next function but the object is disabled so u get null. try calling display total coins and after it disable the object.