- Home /
How can I enable an object from a script?
Basically i want object1 to be able to make object2 .enabled = true(object2 starts out with .enabled = false).
I've tried setting the game object to a variable with GameObject.Find("object2"), but when I set it to enabled = true in a OnTriggerEnter function object2 and its scripts aren't activated in the scene.
Answer by TonyLi · Jan 16, 2014 at 04:13 PM
Use GameObject.SetActive(). (See Activating GameObjects.)
var object2 = GameObject.Find("object2");
if (object2 == null) {
Debug.LogError("object2 not found");
} else {
object2.SetActive(true);
}
The enabled property on behaviours (scripts) only affects the script, not the GameObject itself.
Thank you so much, this exactly the kind of clarification I needed. 10/10
Its registering as not being null but it isn't actually activating object2. any advisement?
$$anonymous$$aybe object2's parents are inactive. You can see if object2 is active in the hierarchy (that is, all its parents are active, too), by checking the value of activeInHierarchy.
It's also possible that the GameObject is active but some of the components or behaviours have been disabled. When you play the scene and get to the point where it runs object2.SetActive(true), pause the game and inspect object2. $$anonymous$$ake sure it's active in the hierarchy and that all the expected components are enabled (ticked).