Search Unity

"Object reference not set to an instance of an object" error when trying to GetComponent<Text>

Discussion in 'UGUI & TextMesh Pro' started by Wailor, May 5, 2015.

  1. Wailor

    Wailor

    Joined:
    May 2, 2015
    Posts:
    5
    I want to store a button text in a Manager script.

    The thing is, for some reason, when the code is executed, I get the error "Object reference not set to an instance of an object".


    My code is the following (the line with the red spot is the one that brings the error)



    The hierarchy I have is this:


    And the "TitleButtonWhateverText" GameObjects also have a Text component:



    Oh, I also tryied with this code, but I still get the same.



    I Googled it until I got blood in my fingertips (quite exagerated, I know) and got nothing.

    Could somebody help me? This is driving me crazy o_O

    EDIT: forgot to mention that, if I comment the red spoted line, then the last one brings the error.
    EDIT 2: I checked if the problem was finding the game object, but it does it well.
     
    Last edited: May 5, 2015
  2. Wailor

    Wailor

    Joined:
    May 2, 2015
    Posts:
    5
    I found that the error is that I didn't assigned a value to the variable buttonsText. I guess I got used to C++

    Could a moderator close this thread, please?