- Home /
Can a disabled GameObject re-enable itself?
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...)
Answer by Kourosh · Sep 22, 2011 at 07:19 AM
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.
Thanks! $$anonymous$$aybe I'll just write something ugly and gross that turns off all the components of an object and all its children.....
No, this answer is actually wrong, see correct one below
Answer by mrDude · Sep 22, 2011 at 08:27 AM
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...
https://docs.unity3d.com/ScriptReference/GameObject-activeInHierarchy.html This might be an interesting read for checking if the child is active or inactive because of the parent game object.
Answer by AurraSing · Jun 23, 2019 at 07:10 PM
You can enable your gameobject using: Invoke or InvokeRepeating
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ReEnableSelf : MonoBehaviour { // Start is called before the first frame update void Start() { InvokeRepeating("reEnableSelf", 3f, 3f); }
void reEnableSelf() { gameObject.SetActive(!gameObject.activeSelf); if(gameObject.activeSelf) { Debug.Log(gameObject.name + " is active!!"); } }
}
yeah but what if it's the same object that has to reenable or re-activate itself. when the object is disabled, so is the script. this won't help imo.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Material doesn't have a color property '_Color' 4 Answers
Is LateUpdate() always called, even if the script is disabled in the Inspector? 1 Answer