Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

}
share|improve this question
1  
Err, what's wrong exactly? Doesn't compile? Doesn't run? Gives wrong result? –  Laurent Couvidou Nov 12 '12 at 16:47
add comment

closed as too localized by Byte56, Josh Petrie, Jonathan Hobbs, 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.

2 Answers

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
add comment

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
add comment

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