- Home /
Timer doesn't count
I'm trying to make a countdown timer. I'm not sure what I'm doing wrong. I have a GUI box that counts score (that part works as it should) and then in the same box it is supposed to have a countdown to the end of the game. I have the timer in two scripts
Timer.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Timer : MonoBehaviour {
System.Timers.Timer TheTime;
public static float time = 100;
public GameObject Win;
GameObject WinClone;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
time -= Time.deltaTime;
if (time <= 0) {
for (int i = 0; i <= KeepScore.Score; i++) {
System.Random rnd = new System.Random();
int x = rnd.Next(-17,17);
int z = rnd.Next(-17,17);
WinClone = Instantiate (Win, transform.position, Quaternion.identity) as GameObject;
}
}
}
}
KeepScore.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KeepScore : MonoBehaviour {
public static int Score = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUI.Box (new Rect (50, 50, 100, 100), "Score: " + Score.ToString () + "\nTime: " + Timer.time);
}
}
Does anyone know what I'm doing wrong? The timer stays at 100 even when the score in increasing.
If the Timer script is attached on a GameObject, which is Active in Hierarchy, and the Timer script is enabled, Update()
would be called, and the time variable would decrease.
So, either the above is not true, or the time
variable is being reset to 100 elsewhere in your code.
Your answer
Follow this Question
Related Questions
Moving from Java to Unity 4 Answers
LineRenderer static movement 1 Answer
How to work a real life timer? 2 Answers
Distribute terrain in zones 3 Answers
Attacking Enemies at an Angle 0 Answers