- Home /
How to Find and enable object very quickly
In my scene I have one script for all triggers. When player come inside one of the triggers, I want to enable some object (smoke), and then disabje object, which contain current script.
To access smoke I use Find and when I try to enable it, I got error: "Object reference not set to an instance of an object"
May be it because Find need more time to find it? I can't use public GameObject, because I use this script in many places in my game.
void OnTriggerEnter()
{
if(nameOfObjectWithTrigger == "RedButtonTrigger")
{
//..
}
else if(nameOfObjectWithTrigger == "WaterTrigger")
{
//..
}
else if (nameOfObjectWithTrigger == "MiddleCube")
{
GameObject smoke = GameObject.Find("SmokeDoor");
smoke.SetActive(true); // Error
gameObject.SetActive(false);
}
}
Answer by Dave-Carlile · Jun 18, 2015 at 04:52 PM
Your Find
call isn't finding SmokeDoor
. Either because it doesn't exist, or it's already inactive. Find
won't find an object that isn't already active.
Since you already know what object you want to activate, you should be able to set up a reference to it on the script and establish that relationship in the editor by dragging the SmokeDoor
object to it.
Answer by jenci1990 · Jun 18, 2015 at 04:51 PM
you can't find inactive object. Use static variabe:
public static GameObject smokeDoor;