Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This was originally posted as a follow-up comment to http://stackoverflow.com/questions/30714357/guitext-in-unity-5-need-an-alternativehttps://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.

This was originally posted as a follow-up comment to http://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.

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.

Source Link

Creating a GUI Countdown Timer Unity5

This was originally posted as a follow-up comment to http://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.