Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 {

    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) {
            } else {
                msg.guiText.text = "You think you can do better? Press 'R' to Try again!";
                if(Input.GetKeyDown(KeyCode.R)) {

        if(!Input.GetKey(KeyCode.Space) & starting) {
            end = true; 

    IEnumerator timer() {
        isTimeDone = false;
        yield return new WaitForSeconds(1);
        isTimeDone = true;

share|improve this question

closed as too localized by Byte56, Josh Petrie, doppelgreener, Nate, Laurent Couvidou Nov 21 '12 at 14:19

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Err, what's wrong exactly? Doesn't compile? Doesn't run? Gives wrong result? – Laurent Couvidou Nov 12 '12 at 16:47

Unless there's other ocde you're not showing us, isTimeDone is never initialized to true (bools are false by default), so your StartCoroutine(timer()); never gets called.

When you're running into issues like this, the best thing to do is attach the debugger to it. Barring that, put debug prints on lines you think should be getting hit with the values of variables as they currently are so you can see the program flow.

share|improve this answer

Well I'm not exactly sure what `StartCoroutine` does (I assume it is a form of asynchronous function execution) so I'll skip that. It looks like your problems stem from the fact that you never set `starting` back to false. You should probably add that to your second if statement.

On top of that. you never actually set the value `isTimeDone` to true outside of the method that only gets called inside the if statement that relies on the true value. You should initialize this value to true in your behaviors constructor.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.