2
\$\begingroup\$

I'm currently learning game development in Unity from this course on Lynda.com. Currently I'm trying to displaying the time remaining in the game after it has been set to 5 minutes initially. When I look at the scene, I can see the text for the timer displayed in the top left corner of the canvas, but when I run the game, I'm not seeing it at all.

I made a script for a game manager which is derived from a Singleton class. The game manager contains a private variable (and an accessor method) for the time remaining. I have another script that accesses the value for the time remaining and displays it on screen. In the Unity editor, I added a UI game object for the text box and then added the text box to the GUI representation of the timer label attribute. It seems like everything should be working but since I'm still very new to this, I'm probably missing something simple. Here is the code for both scripts:

GameManager.cs

public class GameManager : Singleton<GameManager> {
    private float _timeRemaining;

    public float TimeRemaining
    {
        get { return _timeRemaining; }
        set { _timeRemaining = value; }
    }

    private float maxTime = 5 * 60; // In seconds.


    // Use this for initialization
    void Start () {
        TimeRemaining = maxTime;
    }
    // Update is called once per frame
    void Update () {
        TimeRemaining -= Time.deltaTime;
        if(TimeRemaining <= 0)
        {
            //Now Deprecated
            //Application.LoadLevel(Application.loadedLevel);
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            TimeRemaining = maxTime;
        }
    }
}

UpdateUI.cs

public class UpdateUI : MonoBehaviour {

    [SerializeField]
    private Text timerLabel;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        timerLabel.text = FormatTime(GameManager.Instance.TimeRemaining);
    }

    private string FormatTime(float timeInSeconds)
    {
        return string.Format("{0}:{1:00}", Mathf.FloorToInt(timeInSeconds / 60), Mathf.FloorToInt(timeInSeconds % 60));
    }
}

EDIT:

Canvas Settings in the inspector

enter image description here

Image of the Time Remaining displayed in the scene enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ Can you post screenshot of game and your canvas settings? \$\endgroup\$ Commented Jan 21, 2016 at 21:04
  • \$\begingroup\$ Is the textfield visible but not updating the text or not visible at all? \$\endgroup\$
    – Vaillancourt
    Commented Jan 21, 2016 at 22:02
  • \$\begingroup\$ @HamzaHasan I just added the image \$\endgroup\$ Commented Jan 21, 2016 at 22:36
  • \$\begingroup\$ @AlexandreVaillancourt The textfield is visible in the scene (as seen in the second image) but its not visible in the actual game. It seems that the actual text object itself is not docking to the Screen as every time I try to orbit around it, it appears as though its farther into the scene on the Z axis. I'm not sure if it matters but that could be causing a problem. \$\endgroup\$ Commented Jan 21, 2016 at 22:40

1 Answer 1

2
\$\begingroup\$

Well, there is nothing obvious problem I can find in your settings but here is my recommended settings, so you can match it, may be your problems lies in between these.

  • Text Object should be like,

Text Object

  • Canvas settings

Canvas settings

  • Game resolution should not be set to Free Aspect. Assign some resolution.

game resolution

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, I got some other text to display on the canvas so the problem is probably just specific to that component. \$\endgroup\$ Commented Jan 22, 2016 at 6:37
  • \$\begingroup\$ Create a new one then :D \$\endgroup\$ Commented Jan 22, 2016 at 6:38
  • \$\begingroup\$ or duplicate the working one \$\endgroup\$ Commented Jan 22, 2016 at 6:39

You must log in to answer this question.

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