Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chellebell1689 · May 26, 2014 at 07:28 PM · timer-script

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chellebell1689 · May 27, 2014 at 12:36 AM 0
Share

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!

avatar image Kiwasi · May 27, 2014 at 12:50 AM 0
Share

Try using the keyword bool ins$$anonymous$$d boolean on line 2 of this answer.

avatar image Chellebell1689 · May 27, 2014 at 01:08 AM 0
Share

That worked! Thank you so very much!!

avatar image Kiwasi · May 27, 2014 at 01:33 AM 0
Share

Be sure to mark the answer as accepted.

avatar image meat5000 ♦ · May 27, 2014 at 01:39 AM 0
Share

Yeah sorry, I don't really do much C# :D

Writing in JS for months fries one's brain :D

avatar image
1
Wiki

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;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chellebell1689 · May 26, 2014 at 08:33 PM 0
Share

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?

avatar image meat5000 ♦ · May 26, 2014 at 08:56 PM 0
Share

Add a boolean trigger, declared in Class variables.

avatar image Chellebell1689 · May 26, 2014 at 09:30 PM 0
Share

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. :)

avatar image Kiwasi · May 26, 2014 at 10:45 PM 0
Share

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 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I implement a timer into this script that will record the total time the player has played. 2 Answers

Checking the value of null 2 Answers

How to set count-down timer for a muli-player game in C# UNET? The timer must only start when all the players are connected. 1 Answer

Game automatically loses before the timer starts 1 Answer

How to create a Game Time in mm:ss format 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges