can't spot the error. Trying to increment
- by Kevin Jensen Petersen
I really can't spot the error, or the misspelling. This script should increase the variable currentTime with 1 every second, as long as i am holding the Space button down.
This is Unity C#.
using UnityEngine;
using System.Collections;
public class GameTimer : MonoBehaviour {
//Timer
private bool isTimeDone;
public GUIText counter;
public int currentTime;
private bool starting;
//Each message will be shown random each 20 seconds.
public string[] messages;
public GUIText msg;
//To check if this is the end
private bool end;
void Update () {
counter.guiText.text = currentTime.ToString();
if(Input.GetKey(KeyCode.Space)) {
if(starting == false) {
starting = true;
}
if(end == false) {
if(isTimeDone) {
StartCoroutine(timer());
}
} else {
msg.guiText.text = "You think you can do better? Press 'R' to Try again!";
if(Input.GetKeyDown(KeyCode.R)) {
Application.LoadLevel(Application.loadedLevel);
}
}
}
if(!Input.GetKey(KeyCode.Space) & starting) {
end = true;
}
}
IEnumerator timer() {
isTimeDone = false;
yield return new WaitForSeconds(1);
currentTime++;
isTimeDone = true;
}
}