Issue in gameObject.SetActiveRecursively


I am enabling a series of game objects with gameObject.SetActiveRecursively(true);

its something like :

void Activate()

    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

ah i think i know where your problem is it should be this.gameObject.SetActiveRecursively(true)

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.

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.