can't spot the error. Trying to increment

Posted by Kevin Jensen Petersen on Game Development See other posts from Game Development or by Kevin Jensen Petersen
Published on 2012-11-12T16:35:06Z Indexed on 2012/11/12 23:15 UTC
Read the original article Hit count: 334

Filed under:
|

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;
    }

}

© Game Development or respective owner

Related posts about c#

Related posts about unity