Skip to main content
Please select the whole code block and use the code block option in the editor, it looks prettier that way ;)
Source Link
John Hamilton
  • 3.1k
  • 1
  • 16
  • 34

In my game you have 2 minutes to finish the whole game and after searching for an answer to my problem I think I should use DontDestroyOnLoad but I can't understand how to use it correctly. Here's the code:

public class countdownTimer : MonoBehaviour {

    public float myTimer = 120;
    public Text timerText;
    private bool timerIsActive = true;

    void Start () {
        timerText = GetComponent<Text> ();
    }
    
    void Update () {
        if (timerIsActive) {
            myTimer -= Time.deltaTime;
            timerText.text = myTimer.ToString ("f0");
            print (myTimer);

            if (myTimer <= 0) {
                myTimer = 0;
                timerIsActive = false;
                SceneManager.LoadScene ("lose");

            }
        }

    }
}

}

In my game you have 2 minutes to finish the whole game and after searching for an answer to my problem I think I should use DontDestroyOnLoad but I can't understand how to use it correctly. Here's the code

public class countdownTimer : MonoBehaviour {

public float myTimer = 120;
public Text timerText;
private bool timerIsActive = true;

void Start () {
    timerText = GetComponent<Text> ();
}

void Update () {
    if (timerIsActive) {
        myTimer -= Time.deltaTime;
        timerText.text = myTimer.ToString ("f0");
        print (myTimer);

        if (myTimer <= 0) {
            myTimer = 0;
            timerIsActive = false;
            SceneManager.LoadScene ("lose");

        }
    }

}

}

In my game you have 2 minutes to finish the whole game and after searching for an answer to my problem I think I should use DontDestroyOnLoad but I can't understand how to use it correctly. Here's the code:

public class countdownTimer : MonoBehaviour {

    public float myTimer = 120;
    public Text timerText;
    private bool timerIsActive = true;

    void Start () {
        timerText = GetComponent<Text> ();
    }
    
    void Update () {
        if (timerIsActive) {
            myTimer -= Time.deltaTime;
            timerText.text = myTimer.ToString ("f0");
            print (myTimer);

            if (myTimer <= 0) {
                myTimer = 0;
                timerIsActive = false;
                SceneManager.LoadScene ("lose");

            }
        }

    }
}
Source Link

Keep the timer counting after changing scenes

In my game you have 2 minutes to finish the whole game and after searching for an answer to my problem I think I should use DontDestroyOnLoad but I can't understand how to use it correctly. Here's the code

public class countdownTimer : MonoBehaviour {

public float myTimer = 120;
public Text timerText;
private bool timerIsActive = true;

void Start () {
    timerText = GetComponent<Text> ();
}

void Update () {
    if (timerIsActive) {
        myTimer -= Time.deltaTime;
        timerText.text = myTimer.ToString ("f0");
        print (myTimer);

        if (myTimer <= 0) {
            myTimer = 0;
            timerIsActive = false;
            SceneManager.LoadScene ("lose");

        }
    }

}

}