- Home /
How do I make my timer continue to count? (C# Script)
I made a little maze game and I want to time the player to see how long it takes for them to get to the end. I have them start at a bright green wall and I tried to set it up so that when they run into the wall, it disappears and the timer starts. Then when they find the red wall, the timer stops and "You Win!" pops up. I got the walls to disappear like I want, and the timer and "You Win!" text to show, but I can't get the timer to continue. When you run into the green wall, the time updates once but that's it. Below is the script I used, I used C# because that's just what I was using in a tutorial. Sorry in advanced if it's all crazy I probably took the long way around. lol
public GUIText timerText;
public GUIText winText;
private float timespent = 0.0f;
private float seconds = 0.0f;
private float minutes = 0.0f;
void Start ()
{
SetTimerText ();
winText.text = "";
}
// Update is called once per frame
void Update ()
{
timespent += seconds;
seconds += Time.deltaTime;
if (seconds > 60)
{
minutes += 1;
seconds = 0;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Start")
{
other.gameObject.SetActive (false);
timespent = seconds + 1;
SetTimerText ();
}
if (other.gameObject.tag == "RedDoor")
{
other.gameObject.SetActive (false);
timespent = seconds + 0;
SetTimerText ();
winText.text = "You Win!";
}
}
void SetTimerText ()
{
timerText.text = "Time: " + minutes + "" + seconds + timespent.ToString ();
}
}
Sorry again, I couldn't figure out how ppl got it to show up as if you were looking in the script program...newbie here lol
Answer by meat5000 · May 26, 2014 at 10:21 PM
I've left out most of the code, just left enough to show you where to put things. Something like this, using the boolean to switch the timer on and off:
private float minutes = 0.0f;
private boolean timerTrigger = false;
void Update ()
{
if(timerTrigger)
{
timespent += seconds;
seconds += Time.deltaTime;
if (seconds > 60)
{
minutes += 1;
seconds = 0;
}
}
}
~~~
if (other.gameObject.tag == "Start")
{
timerTrigger = true;
~~~ ~~~
if (other.gameObject.tag == "RedDoor")
{
timerTrigger = false;
~~~
It may not work exactly as I've done it but its to show you the idea.
Reset your timer somewhere with
timespent = 0.0;
Thank you for explaining that. I like the boolean, it seams simple (at least compared to what I was trying to do). $$anonymous$$y only problem is it's now saying I need a "directive" or "assembly" to use the boolean. I tried to look it up in the directory, but there's too many and I'm too new to know which one to use. Thank you so much for your help!
Try using the keyword bool
ins$$anonymous$$d boolean
on line 2 of this answer.
Yeah sorry, I don't really do much C# :D
Writing in JS for months fries one's brain :D
Answer by Kiwasi · May 26, 2014 at 07:35 PM
Edit: Answer updated based on comments
// Add this line to your variable declarations
Private bool hasTimerStarted = false;
// Add this code to your Update() method
if(hasTimerStarted){
//Use your existing update method to update the timer
SetTimerText();
}
// Add this to your OnTriggerEnter() Method
// Inside the Start if
TimeSpent = 0;
hasTimerStarted = true;
// Inside the RedDoor if
hasTimerStarted = false;
Not sure if you saw my original post, I accidentally set it as answer ins$$anonymous$$d of comment, I think it was deleted... That got the timer to start and show on the screen (thank you very much!), but how do I get it to start only once the player has collided w/ the set trigger, Start, and then stop when once collided w/ the set trigger, Stop?
I'm sorry, I have no idea what that is or how to do that. I have taught myself a little scripting and will be starting school in the next week/two to learn how to program & develop games. I appreciate all the help, and I'm sorry if frustrating anyone. :)
I've added some code to my answer. Which is in essence the same as what @meat5000 just posted. Teach me to post without refreshing my browser :)