Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't GameObject.Find() on UI Text

Discussion in '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()*
     
  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:
    18
    As krrisztian said, that GetComponent part on the end helped me out with my problem too.
     
  5. Bioinformatizer

    Bioinformatizer

    Joined:
    Nov 27, 2014
    Posts:
    5
    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
     
    BlackJuice and Shodan0101 like this.
  13. FelixJones

    FelixJones

    Joined:
    Sep 13, 2017
    Posts:
    1
    End bit helped me too...lol
     
  14. wanrclhnyd

    wanrclhnyd

    Joined:
    Oct 18, 2018
    Posts:
    3
    and early 2020, i found this thread helped me.. thanks!
     
  15. hichemHse

    hichemHse

    Joined:
    Feb 21, 2018
    Posts:
    1
    you can do other think to find Text :
    public GameObject obj;
    public Text txt;

    void Start(){
    obj= GameObject.Find("name_of_obj_in_scene");
    txt= txt.GetComponent<Text>();
    }
    // it's same object , first declare as gameObject and after getComponent as Text
    Good Luke
     
  16. HumanOfCult

    HumanOfCult

    Joined:
    Feb 4, 2020
    Posts:
    1
    duuude you're genius
     
  17. nikothegreek

    nikothegreek

    Joined:
    Jan 14, 2020
    Posts:
    1
    Same!
     
  18. markuskir

    markuskir

    Joined:
    Mar 2, 2018
    Posts:
    11
    Yet another person helped by this thread. GetComponent<Text>()..
     
  19. indyROD

    indyROD

    Joined:
    Apr 8, 2020
    Posts:
    4
    First use :
    Code (CSharp):
    1. using UnityEngine.UI;
    init your var :
    Code (CSharp):
    1. public GameObject Canvas; //Var to get the player UI Canvas
    And use this where you want (function or Update() for ex)
    Code (CSharp):
    1. Canvas = GameObject.Find("PlayerUI"); // PlayerUI  : my Player UI Canvas
    2. foreach (Transform eachChild in Canvas.transform) {
    3.     if (eachChild.name == "Score") { // Score : my GameObject where is the Text with the Player Score
    4.         foreach (Transform eachChild2 in eachChild.transform) {
    5.             if (eachChild2.name == "points") { // The Child of Score : the Text object...
    6.                 //Debug.Log ("Child found. Name: " + eachChild2.name);
    7.                 Score = eachChild2.GetComponent<Text>();
    8.             }
    9.         }
    10.     }
    11. }
    So now you don't have to drag & drop a txt GameObject (UI) into a [SerializeField] var

    I've spend hours, like you, to find a solution. Maybe we can do a recursive function to simplify...
     
    Last edited: Apr 18, 2020
  20. mustafaadwi

    mustafaadwi

    Joined:
    Dec 15, 2020
    Posts:
    1
    For those comming from 2020 :D:D:D make sure that you use Text and not Text mesh pro in order to use GameObject.Find("Name of text in scene hirarchy").GetComponent<Text>().text = "your new text";
     
  21. XhryZed

    XhryZed

    Joined:
    Jun 16, 2019
    Posts:
    2
    still helping people in 2021!
     
  22. maxburnheart_unity

    maxburnheart_unity

    Joined:
    Oct 19, 2021
    Posts:
    1
    It's amazing how this thread helped so many people, myself included. Especially because that wasn't even intended. xD
     
    Last edited: Oct 22, 2021
    indyROD likes this.
  23. indyROD

    indyROD

    Joined:
    Apr 8, 2020
    Posts:
    4
    Yes of course ;-)

    The aim is just to find an object...

    in this case :
    Code (CSharp):
    1. Score.text = "0000" + (pointsCount+10).ToString();
     
  24. CaJaske

    CaJaske

    Joined:
    Jan 2, 2018
    Posts:
    1
    For those of you who need this:

    Regular text:

    Code (CSharp):
    1. GameObject.Find("yourObjectName").GetComponent<Text>().text = "Heyo";
    ---------------------------------------

    TextMeshPro text:

    Code (CSharp):
    1. GameObject.Find("yourObjectName").GetComponent<TextMeshProUGUI>().text = "Heyo";
    You have to be on at least Visual Studio 2017 15.7.5.
     
    SmithySFC likes this.
  25. Lord7even

    Lord7even

    Joined:
    Feb 20, 2022
    Posts:
    1
    Even in 2022 this helped me out!
     
  26. Byakkomao

    Byakkomao

    Joined:
    Aug 11, 2022
    Posts:
    1
    Thanks a lot, now i know how to link two objects in private!!! i knew how to call it but if i have many objects of the same type with different names i was so lost, if anyone is interested, this is how it went for me based on op's code:

    private Text text;


    void Start() {

    text = GameObject.Find("Score").GetComponent<Text>();


    }

    that way i can find any thing no matter how many objects of the same type i have on scene , thanks a lot mate! this threat was really helpfull for me
     
  27. zhuoyi

    zhuoyi

    Joined:
    Sep 19, 2022
    Posts:
    9
    This still helped me in 2023.
     
  28. FalkPretschner

    FalkPretschner

    Joined:
    May 13, 2023
    Posts:
    1
    Thx helped a lot The
    .GetComponent<Text>();
    Part