- Home /
Countdown
#pragma strict
var timer : float = 10.0;
var lsCanvas : Canvas;
function Start () {
timer -= Time.deltaTime;
if(timer <= 0)
{
timer = 0;
lsCanvas.enabled = true;
}
}
What´s wrong with this script?
Do you want to enable the canvas after 10 seconds?
Start function only executes for 1 frame only. Therefore timer -= Time.deltaTime; will execute once only. And timer will never reach zero.
Answer by Landern · Jan 08, 2015 at 03:56 PM
The Start function is called when an object is enabled, Time.deltaTime is the time between frames. If your timer is 10 and start is called once, in the frame when the GameObject is enabled, then your if statement will always result in false and lsCanvas.enabled is never be set. Did you mean to put this in the Update function which is called every frame?
If you did mean to put it in Update, you should also constrain the overall process with a boolean so it's not evaluated over and over again
Answer by hypnoticmeteor · Jan 08, 2015 at 09:36 PM
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
public Canvas canvasOne;
IEnumerator Start () {
canvasOne.gameObject.SetActive(false);
yield return new WaitForSeconds(10f);
canvasOne.gameObject.SetActive(true);
}
}
Your answer
Follow this Question
Related Questions
Mini Game Timer - Unity 2017 0 Answers
How to make a simple battery? 0 Answers
Countdown/timer in Javascript 3 Answers
Timer Between Labels 2 Answers
Need some help an object collider that stops a timer then displays it on another scene 1 Answer