Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Can't GameObject.Find() on UI Text

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by specterdragon, Jan 23, 2015.

  1. specterdragon

    specterdragon

    Joined:
    Dec 30, 2013
    Posts:
    21
    I'm sure that this is a simple question regarding the new UI system... One that's probably been asked a million times, but I can't seem to locate the answer by searching the forums :( (My search-fu is lacking)...

    I have a Text object in my canvas and a script attached to a game manager (empty) object to deal with various things. For whatever reason, the following code is failing with a null reference exception in the Start function (which of course means that ClickedButton will also fail). I've double checked all of the object names with no success. Is it not possible to use GameObject.Find with the new UI components? Or is there something I'm missing?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class MenuManager : MonoBehaviour {
    6.    
    7.     public Text my_text;
    8.    
    9.     void Start() {
    10.         my_text = GameObject.Find("text_object_name").GetComponent<Text>();
    11.     }
    12.    
    13.     public void ClickedButton() {
    14.         my_text.text = "Testing...";
    15.     }
    16.  
    17. }
    18.  
    Any help is appreciated.
     
  2. specterdragon

    specterdragon

    Joined:
    Dec 30, 2013
    Posts:
    21
    *sigh* COMPLETELY disregard!!! (I don't think I can delete the thread)
    Apparently I was in "play" mode while checking the names, so they didn't take. *ForeheadSlap()*
     
    mr_freedo and ozarka30 like this.
  3. krrisztian

    krrisztian

    Joined:
    Mar 16, 2015
    Posts:
    1
    You helped me with the ".GetComponent<Text>()" part of the Start method so this thread was not useless already ;)
    I just couldn't figure out how to .Find a UI Text gameobject...
    ANYWAYS Thanks :)
     
  4. B4ttleCat

    B4ttleCat

    Joined:
    Mar 31, 2014
    Posts:
    7
    As krrisztian said, that GetComponent part on the end helped me out with my problem too.
     
  5. Bioinformatizer

    Bioinformatizer

    Joined:
    Nov 27, 2014
    Posts:
    3
    That sneaky little GetComponent<Text>()...Thanks for this post!
    :)
     
    Emodison likes this.
  6. arspermeable

    arspermeable

    Joined:
    Jan 7, 2016
    Posts:
    1
    The same for me!!!! thanks for your question that became my answer.
    while(true) {ForeheadSlap();}
     
  7. rooksFX

    rooksFX

    Joined:
    Jul 29, 2015
    Posts:
    3
    How can I find a GameOBject with a concatenated string...?
    Something like this
    objectString = "fCanvas/fPanel/galleryContainer/Panel/img" + i;

    GameObject.Find (objectString).GetComponent<RawImage> ().texture = Resources.Load (landmarkConcat) as Texture;

    Those line of codes is in side a loop... Sadly I can't seem to make it work... Got a Object Reference Error in the GameObject.Find line of code...
     
  8. alii_nezamii

    alii_nezamii

    Joined:
    Jul 11, 2016
    Posts:
    1
    I've tested this code works
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;

    4. public class MenuManager : MonoBehaviour {
    5. public Text my_text;
    6. public Button B;
    7. void Start() {

    8. B.onClick.AddListener(() => {

      my_text.text = "Testing...";



      });
    9. }


    10. }
     
  9. MarkDimi94

    MarkDimi94

    Joined:
    Dec 12, 2016
    Posts:
    1
    Made an account just to say you helped me out as well. Same problem. This continues to be relevant.
     
    Emodison likes this.
  10. miniweed

    miniweed

    Joined:
    May 10, 2017
    Posts:
    1
    GameObject.Find("text_object_name").GetComponent<Text>();

    This!!! You help me a lot thanks!
     
  11. chinkx1205

    chinkx1205

    Joined:
    Aug 6, 2018
    Posts:
    1
    Thanks a lot! You r my lifesaver!
     
  12. jonashau18183

    jonashau18183

    Joined:
    May 5, 2019
    Posts:
    1
    and even in 2019, there are still people, who are thankful for your post haha... thx dude
     
    GMax likes this.
  13. ArshidaGames

    ArshidaGames

    Joined:
    Feb 9, 2019
    Posts:
    46
    Code (CSharp):
    1. public class SaveManager : MonoBehaviour
    2. {
    3.     public static SaveManager instance;
    4.     public Text currentScore;
    5.     public Text HighScore;
    6.     public int currentscore = 0;
    7.     public int highscore = 0;
    8.  
    9.     void Awake()
    10.     {
    11.         if (instance == null)
    12.             instance = this;
    13.         else if (instance != null)
    14.             Destroy(gameObject);
    15.  
    16.         DontDestroyOnLoad(gameObject);
    17.     }
    18.     void Start()
    19.     {
    20.  
    21.         HighScore = GameObject.Find("highscore").GetComponent<Text>();
    22.     }
    23. }
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. SaveManager.Start () (at Assets/Scripts/SaveManager.cs:27)
    3.  
    in my game ,it is always missing ...what can be wrong?