Skip to main content
added 277 characters in body
Source Link

##EDIT:

Canvas Settings in the inspector

enter image description here

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

##EDIT:

Canvas Settings in the inspector

enter image description here

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

Fixed grammar and added tag for more visibility.
Source Link

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 first 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 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));
    }
}

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 first 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));
    }
}

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));
    }
}
Source Link

Unity 5 - Time Remaining displaying in scene but not in Game

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 first 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));
    }
}