Reputation: 61
I have a game and I am doing now the UI (GUI) for that game to set some parameters. One of this parameter should be the duration of this game (in minutes). How can I define that when I type for example 3 in the input field , that the game should run 3 minutes?
Thank you for your help!
Edit:
Here the code I have so far:
public void DurationGame() {
float myTimer = 5.0f;
if (myTimer > 0) {
myTimer -= Time.deltaTime;
}
if (myTimer <= 0) {
//Game should stop here
Debug.Log ("GAME OVER");
}
Upvotes: 0
Views: 794
Reputation: 133
You got it, just put that code in the Update function, like this:
public float myTimer = 5.0f;
public bool gameIsRunning; //you don't want to let the timer run while in pause
void Update ()
{
if (myTimer > 0 && gameIsRunning) myTimer -= Time.deltaTime;
else if (myTimer <= 0 && gameIsRunning) Debug.Log ("GAME OVER");
}
Upvotes: 1