- Home /
Enabling a disabled prefab in the scene
hi,
I am loading a prefab into my scene. By default this gameobject is disabled. I am stuck on enabling this gameobject from another script. Since it isn't in the scene by default I cannot make a public gameobject and assign it. I tried getting the gameobject by name and tag but haven't been succesful.
Anyone got any clues on this ?
Thanks
Marc
The most efficient way (i.e. avoiding searching for the object) would be to have a reference to the GameObject as a member of some other object in the scene (perhaps in the script which is going to be used to enable it). This reference would start off as null, and you would set it to your new object when you instantiate it.
"I tried getting the gameobject by name and tag but haven't been succesful."
Just make sure your that Your Prefab loading is done 1st and then find the gameobject. This is the only reason u arent getting the gameobject by name.
It may be worth mentioning one common pitfall when searching for a runtime-instantiated object by name, which is failing to find it due to searching for the name of the Prefab. Ins$$anonymous$$d you should explicitly set the name after instantiation, and use that name in your search. But better to avoid searching altogether as per my previous comment.
Thanks, I fixed it by creating a new gameobject as a parent. This one is always active and used for referencing. I then just toggled the child on and off. It is still done by searching for now, but I will get back on that when optimizing. Thanks for the help guys !
Answer by M1xelated · Mar 11, 2015 at 02:23 PM
I fixed it by creating a new gameobject and setting the gameobject that I want to disable as its child. This apparantly is the only option when trying to enable it when not instantiated at start
I had a case where I had an active Prefab but one of its children should be disabled by default when instantiated. Then I needed to activate that Child later in the game. Since it is impossible to "Find" an inactive Child or assign a component of a Prefab to a variable before that Prefab is instantiated, the only way I could solve this was to leave the Child active in the Prefab, attach a script to the Child or Parent that assignes the Child to a variable in a script of a Parent and then disables the Child.
Answer by hav_ngs_ru · Mar 11, 2015 at 10:09 AM
public class AnotherScript {
public GameObject gameObjectToEnable; // assign it in inspector
public Start() {
// for testing we activate it iat startup.
// later write it where it shoule be written
gameObjectToEnable.SetActive(true);
}
}
then just add some GO in you scene, attach this script to it, drag-drop your "GO from prefab" to gameObjectToEnable field and start a game.
I guess some confusing between prefab and gameobject in scene. so if this will not work - show us your project.
But the OP says that the object "isn't in the scene by default" and hence "I cannot make a public gameobject and assign it" which I think is explicitly ruling out your solution. I think that the object is being instantiated at run time here.
"Since it isn't in the scene by default I cannot make a public gameobject and assign it" The thing is that the GameObject is not loaded into the scene from the start. I did however find the solution. I created a parent that is always on and its child can be toggled. I googled it and it appears that there is no way to search for disabled gameobjects.
Thanks anyways !