Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting access to a specific game object in C# code

Discussion in 'Scripting' started by rickblacker, Jul 31, 2014.

  1. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    All

    I have a situation (i'm sure very common) where on my heirarchy I have a game object. It has a child game object. Lets call them parentGO and childGO. There will only be 1 instance of these two objects in the entire game. Neither of which are scripts but rather real game objects.

    I have a GameController C# script that is NOT attached to either. From within the GameController script, how do I get a a reference to the childGO. Not a generic game object, but the actual childGO and it's type?

    I thought you could do something like GameObject.FindWithTag( "aTag" ) as TypeOfChildObject

    But, I'm getting compile time errors saying that I can't cast it. I realize that FindWithTag is going to return a generic GameObject, and that being the case, how do you actually get a reference to childGO?

    Thanks for any assistance!!
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    GetComponentInChild?
    Do you want the Child gameObject inside the Parent GameObject?

    You can just do:

    private GameObject _childObject;

    void Start()
    {
    _childObject = GameObject.Find("NAME");
    }
     
  3. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    public GameObject childGO;

    and then just drag and drop it in the inspector
     
  4. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Hi Dublin
    That won't work in my situation. It should I know but it does not.
    Right, but that will just return a generic game object. I need it to be a specific type. Say, GUIText.
     
  5. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Tried, it wont work for what I'm trying to accomplish.
     
  6. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    You can have a component in your childGO set a public variable in your game controller on awake or start. You only need to have a reference to the instance of the GameController.
     
  7. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    I think the issues you are having is from some other part of the scripts and not what you are trying to achieve here all these methods should work really .. just what I would guess without knowing more
     
  8. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    There is no such thing as a "specific type" of GameObject. There is just a GameObject, with different components attached. Yours sounds like you have a GameObject with a GUIText component attached. So you'd have to do:

    Code (csharp):
    1.  
    2. GameObject.FindWithTag( "aTag" ).GetComponent<GUIText>();
    3.  
    Or make a field "public GUIText myText" and drag and drop the object onto it.
     
  9. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Oh is that what he wanted?

    Then yeah...

    private GUIText _myGUI;

    void Start()
    {
    _myGUI = GameObject.Find("GUITEXTOBJECT IN SCENE").GetComponent<GUIText>();
    }

    now you can do:

    _myGUI.text = "yay";