- Home /
Enabling an object that is disabled from the start?
If I create an object in the scene editor and set it to be disabled from there, is it possible to activate it from a script? It doesn't seem possible to find game objects that are already disabled as far as I can see.
Running GameObject.Find("MyDisabledObject")
returns null for disabled objects when I try it. So it's not possible to run SetActive(true)
on it afterwards.
Do all objects placed in the scene from the editor simply have to be enabled from the start?
Answer by puppeteer · Aug 26, 2015 at 12:46 PM
Inactive objects can only be "detected" in the scene through colliders.
My suggestion is to change your design so that you don't deactivate the object itself, but the components that make it visible or interactive. Disable its script if you don't want it to interact with anything, and disable its renderer if you don't want it to be visible. Also disable the collider if you don't want that to affect anything.
Answer by psycocrusher · Nov 29, 2014 at 08:29 PM
You can parent the object and run SetActive on its child.
Thanks. You mean like GameObject.Find("Parent").transform.FindChild("$$anonymous$$yDisabledObject").gameObject.SetActive(true)
? It feels like a ridiculously long work around, but it works!
This here saved me a lot of work, thanks buddy!
It's really works!!So Cool!!! $$anonymous$$any thanks for your $$anonymous$$ching!
Answer by $$anonymous$$ · Nov 29, 2014 at 10:11 PM
The part of finding a disabled object i don't know, just do a public variable. And to activate the object you need to do YourObject.SetActive(true); or false if you wand to unable;
Putting an object in a public variable also works, as long as the script is not part of the same object. Thanks.
Answer by ProbePLayer · May 18, 2019 at 03:22 PM
One way is to have a reference to it in the inspector before the game is started. Then you have access to it regardless whether it's disabled.
Other way is to place the deactivated object as a child of another parent (active) object, and traverse the hierarchy of that parent object with parent.GetComponentsInChildren<MyDisabledObjectScript>(true)
. Note the boolean argument, this has to be true to get deactivated objects as well.
If you don't have your own custom script on that disabled object, you can search for any component that your disabled object has like so: parent.GetComponentsInChildren<Rigidbody>(true)
, and then do a foreach
loop on the returned collection to find the disabled object , like so:
Transform[] childrenComponents = parent.GetComponentsInChildren<Transform>(true);
foreach (Transform child in childrenComponents) {
if (child.name == "DisabledObject") {
// we found the desired object
return child.gameObject;
}
}
And yet another way is to use the Resources.FindObjectsOfTypeAll()
function. This function will find all objects of a specified type, even deactivated ones. After you get the objects of the desired type, you can then search the collection for the desired object. WARNING: this function also gets assets from the Asset folder (meaning all assets), so beware if you manipulate the returned results during runtime, you might modify an asset.
Answer by kylebarker82 · Aug 26, 2015 at 08:25 PM
are you disabling a collider, or deactivating an object? ActiveInHierarchy sounds like what you are looking for, but you're using the word disabled, which is for making a collider unusable, or turning off physics at some point in time.
if (MyDisabledObject.ActiveInHierarchy == false) { MyDisabledObject.SetActive (true); }
if your "disabled" object's name is MyDisabledObject, it's going to look for it by it's name, then turn it active, Write a coroutine if you need the inactive object to wait a while until it becomes visible in the scene.