Search Unity

C# Help - Finding an Object in Hierarchy

Discussion in 'Scripting' started by SpectrumWolf, Nov 22, 2014.

  1. SpectrumWolf

    SpectrumWolf

    Joined:
    Aug 1, 2013
    Posts:
    8
    Right now I'm just how can I find an object in the Hierarchy? I'm rather new to using C#. What I'm trying to also do is update a GUI's text to a new string.

    Code (CSharp):
    1. public class GameTimeScript : MonoBehaviour {
    2.     public float startTime;
    3.     private string currentTime;
    4.  
    5.     void Update()
    6.     {
    7.         startTime -= Time.deltaTime;
    8.         currentTime = string.Format("{0:0.0}", startTime);
    9.         print(currentTime);
    10.  
    11.     }
    12.  
    13. }
    I have my doubts that it's like "Hierarchy.Canvas.Panel.Time" etc.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can find it at runtime with GameObject.Find which finds an object by name or 'GameObject.FindGameObjectWithTag' which finds an object by tag. Both are also avialable as a version to find ALL objects with the respective name/tag, because the first version only returns the first object which will be found.
    There are several other methods which search for type etc.
    Note, that you shouldn't use those in Update routines without any control due to their overhead.

    BUT, as you probably need the reference either way and do not need to find it at runtime, you can set up a variable of the specific type in the script which you use to alter its values. Declare it as a public variable and drag&drop the object on it in the inspector, this way it will be initialized as soon as the game starts to run.

    Example:

    Code (CSharp):
    1. public GameObject myGo;
    If you add that to your script and attach it to a gameObject, you'll see a slot in the inspector and can assign any gameObject from the scene or a prefab from the project folder to it.
    This does of course work with most other types as well.
     
  3. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93

    Is there any specific criterion for the object that you are looking for in the hierarchy, name, type, etc? Here are some options:

    Code (CSharp):
    1. GameObject.Find("name") // Finds a game object by name
    2. GameObject.FindObjectsOfType(typeof(MonoBehaviour)); specific type search
    3. GameObject.FindGameObjectsWithTag("Untagged");  //tag search
    4.  
     
  4. SpectrumWolf

    SpectrumWolf

    Joined:
    Aug 1, 2013
    Posts:
    8
    Code (CSharp):
    1. public class GameTimeScript : MonoBehaviour {
    2.     public float startTime;
    3.     private string currentTime;
    4.     public GameObject timeGUI;
    5.    
    6.     void Start()
    7.     {
    8.         timeGUI = GameObject.Find("Time");
    9.     }
    10.  
    11.  
    12.     void Update()
    13.     {
    14.         startTime -= Time.deltaTime;
    15.         currentTime = string.Format("{0:0.0}", startTime);
    16.         print(currentTime);
    17.         timeGUI.guiText.text = currentTime;
    18.  
    19.     }
    20.  
    21. }
    I used find but now I can't connect to the GUI, please help.
     
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Try "public GUIText timeGUI" instead (you can still click-drag the same gameobject into this slot), and use "timeGUI.text"
     
  6. SpectrumWolf

    SpectrumWolf

    Joined:
    Aug 1, 2013
    Posts:
    8
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameTimeScript : MonoBehaviour {
    5.     public float startTime;
    6.     //public GameObject timeGUI;
    7.     public GUIText timeGUI;
    8.  
    9.     private string currentTime;
    10.  
    11.     void Start()
    12.     {
    13.         timeGUI = GameObject.Find("Time");
    14.     }
    15.  
    16.  
    17.     void Update()
    18.     {
    19.         startTime -= Time.deltaTime;
    20.         currentTime = string.Format("{0:0.0}", startTime);
    21.         // print(currentTime);
    22.         timeGUI.text = currentTime;
    23.  
    24.     }
    25.  
    26. }
    Error on line 13, I'm confused on what you meant.
     
  7. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Change this line to:
    Code (CSharp):
    1. timeGUI = GameObject.Find("Time").GetComponent<GUIText>();
     
  8. SpectrumWolf

    SpectrumWolf

    Joined:
    Aug 1, 2013
    Posts:
    8
    MissingComponentException: There is no 'GUIText' attached to the "Time" game object, but a script is trying to access it.
    You probably need to add a GUIText to the game object "Time". Or your script needs to check if the component is attached before using it.
    GameTimeScript.Update () (at Assets/GameTimeScript.cs:22)
     
  9. SpectrumWolf

    SpectrumWolf

    Joined:
    Aug 1, 2013
    Posts:
    8
    My Text component in the Text GUI is "Text (Script)" but I can't type that in otherwise I'm sure it would error due to the parenthesis.
     
  10. SpectrumWolf

    SpectrumWolf

    Joined:
    Aug 1, 2013
    Posts:
    8
    I got it, turns out 4.6 BETA doesn't let me use the code like GUIText.
     
  11. SpyGamer13

    SpyGamer13

    Joined:
    May 5, 2017
    Posts:
    2
    Hello there, you can fix your prb with "Transform.Find" which how work is on : ".../Documentation/en/ScriptReference/Transform.Find.html"
     
  12. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
  13. NAVANEETHAN007

    NAVANEETHAN007

    Joined:
    Apr 28, 2020
    Posts:
    2
    Verry verry Thank You Bro This Help Is Awsome!
     
    vi_s likes this.
  14. UnderProfile

    UnderProfile

    Joined:
    Mar 17, 2024
    Posts:
    3
    BTW GameObject.FindGameObjectsWithTag returns an array of all the gameobjects with that tag, meaning you cant access the transform and so on of this. If you really need the transform etc. you can use GameObject.FindWIthTag, though this will only find the first one unity sees
     
  15. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,927
    Despite being a necro post, this isn't true at all. Yes you get an array of game objects with said tag, but you can still just iterate the array and access each individual game object and their transform components.

    In any case there's no need to reply to a 10 year old post.
     
    Bunny83 likes this.
  16. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,190
    One of the examples in the docs shows you how but since it's overly complex for what it needed to show here's a simple example of accessing the Transform of each GameObject to reset the position to origin. You're getting an array so anything you would do to other arrays you can do with this one too.

    https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

    Code (csharp):
    1. var someGameObjects = GameObject.FindGameObjectsWithTag("SomeTag");
    2. foreach (var sgo in someGameObjects)
    3. {
    4.     sgo.transform.position = Vector3.zero;
    5. }
     
    Last edited: Apr 7, 2024