Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Getting desperate with the UI Text.

Discussion in 'Scripting' started by Llufes, Jun 25, 2015.

  1. Llufes

    Llufes

    Joined:
    Jan 16, 2015
    Posts:
    6
    Hi,
    First of all this is my first post (So far I've been able to manage everything by myself ).

    So I am trying do display a second counter on my game. So far everything perfect, the code even works really nicely.
    -------------------------------------------------------------------------------------------------------------
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class StatsCode : MonoBehaviour {

    public static float score = 0f;
    public Text text;


    // Use this for initialization
    void Awake () {
    text = this.gameObject.GetComponent<Text>();
    text.text = null;

    }

    // Update is called once per frame
    public void Update () {
    score += Time.deltaTime;
    text.text = score.ToString("F0");
    }
    }
    ------------------------------------------------------------------------------------------------------

    The problem comes when I run the game, I can't avoid getting this error:

    NullReferenceException: Object reference not set to an instance of an object
    StatsCode.Awake () (at Assets/Scripts/StatsCode.cs:14)


    I've tried everything so far... does anyone have an idea about how can I fix this?

    Thank you in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    This error is simply because you have not provided a reference to the Text object via the Unity Editor's inspector. Having not set it in the inspector, when the code runs, it is null.

    Also, please use the code tags (see help area of forums) when posting code for readability.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @Kurt Dekker is right, except this line:

    Code (csharp):
    1.  
    2. text = this.gameObject.GetComponent<Text>();
    3.  
    overwrites whatever is set in the inspector. If you have dragged a specific text component into the inspector field you don't need this line.


    edit: if you aren't setting it in the inspector, this script needs to be on the text child object, not the canvas/gameobject etc. and you keep the line, although you can just have

    Code (csharp):
    1.  
    2. text = GetComponent<Text>();
    3.  
    :)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Good eyes @LeftyRighty... without the code formatting I completely missed that .GetComponent<>() call.

    To add to @LeftyRighty's post above, if you (@Llufes) are doing the .GetComponent<>(), then this script must be on the same GameObject that the Text component is on.
     
  5. Llufes

    Llufes

    Joined:
    Jan 16, 2015
    Posts:
    6
    Thank you all,

    I have checked everything, the gameobject is the one having the script and it is linked in the inspector... However the error keeps apearing.

    The problem is suposed to be on this line:

    text.text = score.ToString("F0");

    On the "text.text" part.
     
  6. pixpusher2

    pixpusher2

    Joined:
    Oct 30, 2013
    Posts:
    121
    I've encountered similar issue before. Most likely the GetComponent<Text> is returning null. Try doing some tests on "text" variable right after this line.
    Code (CSharp):
    1. text = this.gameObject.GetComponent<Text>();
    I don't have a solution other than assigning the text variable through editor
     
  7. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Just as an advice, you should not use text as a var name, as text is already a part of build in components. Anyway, your code is working for me. I just created an Canvas->Panel->Text object, put the text component on it if it wasn't already there and put your script on it. Just changed the score thing to 1f to test, and it counts up every second.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Make sure also that there is not some OTHER game object in your scene that has the script on it also. Sometimes a careless mouse drag will put the script on two objects.

    At the start of the Awake() function,print out the name field (this.name) and make sure the error you see is on the game object you expect it to be,
     
  9. Llufes

    Llufes

    Joined:
    Jan 16, 2015
    Posts:
    6
    Hi all,

    Some times you have the answer in front of you and you can't manage to see it. After all, @Kurt Dekker you were right... Another gameobject had the same script... Thank you all for your help!

    This is an amazing community!
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    You're welcome. I remember the first time that exact bug bit me and it took hours to figure out. Now it is simply one more thing that I check whenever I start getting really weird results like "hey, there IS a Text component, I'm looking at it! What the heck is going on!?"
     
  11. SomeoneWithUnity

    SomeoneWithUnity

    Joined:
    Jul 16, 2020
    Posts:
    1
    i was missing this.