Search Unity

It can't find a gameobject, but it's right there.

Discussion in 'Scripting' started by Redshade, Nov 13, 2017.

  1. Redshade

    Redshade

    Joined:
    Nov 9, 2017
    Posts:
    14
    Here, I have absolutely no idea what is wrong. It seems to work for other people, though.

     
  2. BillJackson

    BillJackson

    Joined:
    Dec 13, 2016
    Posts:
    6
    You need to tell it exactly what SimpleObject is somewhere. It doesn't automatically find it in your hierarchy. One approach would be declaring it at the top like this:

    Code (CSharp):
    1. public GameObject SimpleObject;
    Then assign it to the component in the inspector.

    OR, you could search the hierarchy for it like this:

    Code (CSharp):
    1. GameObject.Find("SimpleObject").GetComponent<NotHotAudio>().AudioPlayer();
    But that's generally not as good for performance and won't tolerate moving things around/renaming/whatever, as well as the first option will.
     
    Last edited: Nov 13, 2017
  3. Redshade

    Redshade

    Joined:
    Nov 9, 2017
    Posts:
    14
    I'm stupid, I should have realized that even as a beginner. I found out myself, though. But I am very pleased and thankful you sent a message about it!