1
\$\begingroup\$

This was originally posted as a follow-up comment to https://stackoverflow.com/questions/30714357/guitext-in-unity-5-need-an-alternative , but I'm asking it here as a dedicated question as per the community's recommendation.

What I'm trying to do: display GUI Text on screen as a countdown timer that counts down in real time

What I've done so far:

  • watched YouTube tutorial: "Unity Scripting Tutorial 7 - A Countdown Timer" by VR Enthusiast.1
  • GameObject --> UI --> Text --> renamed: TimerText
  • Wrote the Timer script in C#

     using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
    
     public class Timer : MonoBehaviour
    
    public float seconds = 59;
    public float minutes = 0;
    
    void Update() {
    
        if (seconds <= 0) {
    
            seconds = 59;
            if (minutes > 1) {
    
                minutes--;
    
            } else {
    
                minutes = 0;
                seconds = 0;
                GameObject.GetComponent<GUIText>().text = minutes.ToString("f0") + ":0" + seconds.ToString("f0");
    
            }
    
        } else {
    
            seconds -= Time.deltaTime;
    
        }
    
        if(Mathf.Round(seconds) <= 9) {
    
            GameObject.GetComponent<GUIText>().text = minutes.ToString("f0") + ":0" + seconds.ToString("f0");
    
        } else {
    
            GameObject.GetComponent<GUIText>().text = minutes.ToString("f0") + ":" + seconds.ToString("f0");
    
        }
    
    }
    

    }

  • placed the TimerText object in the GUI Text object reference

Then I got this error:

"MissingComponentException: There is no 'GUIText' attached to the "TimerText" game object, but a script is trying to access it. You probably need to add a GUIText to the game object "TimerText". Or your script needs to check if the component is attached before using it. Timer.Update () (at Assets/Timer.cs:40)"

I essentially placed The object GUI Text object itself (TimerText) as its own scripting reference in Unity's Inspector. Not sure where to go from here. Any advice on what I can do next? Any help would be appreciated.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

There are basically two ways to access the GUIText.

One way would be a public reference in the class or, like you already did, GetComponent.

The advantage of using a public reference is, that you do not have to attach the timer on the GameObject with the GUIText component. You just declare the variable like so:

public class Timer : MonoBehaviour
{
    public GUIText timerText;

    ...
}

You can then replace all GetComponent calls with timerText. To set this variable, simply drag and drop the GameObject that contains the GUIText component onto the Timer Text field in the Editor.

The GetComponent method, requires you, that the Timer script is attached to the same GameObject that contains the GUIText, or it is called on a GameObject on which has GUIText attached. It is also recommended, to use GetComponent just once, in the Start function of your Timer class, and store it in a variable.

\$\endgroup\$
2
  • \$\begingroup\$ @ user000user : Thanks for the advice, but I'm still unable to display the countdown timer on screen. Every time I try to add the "GUI Text" in the public field in Unity's inspector, it won't allow it (GameObject --> UI --> Text). If I try to add it via AddComponent--> GUIText, it accepts it as a component...but won't display on screen. Any ideas? \$\endgroup\$ Sep 30, 2015 at 10:57
  • \$\begingroup\$ Create a GameObject GO, click 'Add Component' -> Rendering -> GUIText. Now select your GameObject with the Timer component and drag GO on the 'Timer Text' field. Unity detects the GUIText component on GO and will automatically pass it to 'Timer Text'. \$\endgroup\$ Sep 30, 2015 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .