Simple question, but I’m just looking for confirmation - if I disable an object by calling
GameObject.active = false;
is there any way to get it to re-activate itself in a script on the disabled gameobject itself (maybe via a coroutine or some exotica)? I’m guessing the answer is no, but I wanted to get some feedback (the documentation on .active is pretty sparse…)
I think your guess is correct. No, once the gameobject is deactivated the only way to activate it again is to store an instance of it in a GameObject variable and activate/deactivate using that variable.
You should actually notice that when you use Active on components Unity actually throws out a warning at the bottom of your screen saying you should not do this on components but rather use enabled on the gameObject itself.
In all honesty this whole active/enabled thing is really causing me a lot of fuss. i have a component I want to use only at certain times and others that should be active all the time. Thus, dialling the game object makes no sense so I still disable/enable the component and it works fine. I just hope that Unity doesn’t one day say “it’s been depreciated for a long time now, so now we are not going to allow it any more” cause then I’ll be in trouble.
I have found, though, that if you set enabled to false on a gameObject, the contained components are still active and still update my GUI and everything. Really, this whole enabled/active thing really is a pain.
One more example… If you drag a game object onto a script so it can be referenced in the game, but that object starts out disabled, then you will get a runtime error when you try to enable it. Also, if you have a disabled item and you do a gameObject.Find(“child1”) it will not find it so you are not able to activate it that way…
My solution has always been to either drag the child game object to a reference field or to have the script do a .Find() operation and once that is done, I disable it during the parent’s Start() function. Not being able to locate a disabled child object at runtime tends to complicate things a tad…