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 DoodleMumble · May 30, 2014 at 05:47 AM · timertimer-script

Timer will not reset properly.

In the game I am making, there are "dummies" that the player shoots at. When the player shoots them they disappear. The game is a time trial to shoot all of the dummies as fast as possible.

Here is my problem:

I can't get the timer to reset. I have gotten it to stop when all of them are shot, but when i press "R" to reset, the timer goes back to zero when I cross the trigger, but when i finish the map for the second time, the time just adds on with the previous attempt.

I have figured out through hours of research that Time.time is my problem. I cant reset Time.time to zero. I have tried many different things trying to get it to reset correctly and I have run out of ideas.

I am posting my code on this question as well as a YouTube video of me explaining my problem. Thanks in advance for your help :D

YouTube Link:
Shooting Frenzy Help Video

Sorry for the bad audio... it didn't record correctly...

 using UnityEngine;
 using System.Collections;
 
 public class GameControllerTime : MonoBehaviour {
 
     private static int dummyAmount;
     private static float startTime, endTime;
     private static float elapsedTime;
 
     public string currentLevel;
 
     public GUIStyle timer;
     public Rect rect = new Rect(300, 25, 100, 200);
 
     // Use this for initialization
     void Start () {
     
         dummyAmount = GameObject.FindGameObjectsWithTag ("Dummy").Length;
         startTime = 0;
 
     }
 
     public static void RemoveDummy(){
         
         /* I have another script attached to the dummies that triggers this function*/
 
         dummyAmount--;
         
         
         if (dummyAmount <= 0) {
 
             endTime = Time.time;
             elapsedTime = endTime - startTime;
 
         }
         
     }
 
     void Update () {
 
         if (startTime > 0)
         {
             elapsedTime = Time.time - startTime;
         }
 
         /* This reloads the level when "r" is pressed, it could go in another scrpit if needed.
                      It resets the dummies but not the timer. */
 
         if (Input.GetKeyDown (KeyCode.R)) {
 
             Application.LoadLevel (currentLevel);
         
         }
 
     }
 
 
 
     void OnTriggerEnter(){
 
         /* walk through trigger to start timer */
 
         startTime = Time.time;
 
     }
 
     void OnGUI(){
 
         /*puts timer on screen*/    
 
         if (dummyAmount >= 1) {
             GUI.Label (rect, (elapsedTime.ToString ("F2")), timer);
             rect.x = 20f;
             rect.y = 20f;
             }
         else {
             GUI.Label (rect, (endTime.ToString ("F2")), timer);
             rect.x = 20f;
             rect.y = 20f;
         
         }
     }
 
 }
 


Comment
Add comment · Show 2
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 cryingwolf85 · Jun 11, 2014 at 01:51 AM 0
Share

Did my solution work for you?

avatar image DoodleMumble · Aug 29, 2014 at 11:07 PM 0
Share

no, it didn't work.

sorry for taking so long to get back to this... i've had school and such to work on ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cryingwolf85 · May 30, 2014 at 05:55 AM

Ahh, some of this code looks familiar ;) Anyway...

The starting time needs to be set to the current time, so when you subtract the start time from the end time you are getting a meaningful number. Just change line 19 to

 startTime = Time.time;

and your problem is solved.

Comment
Add comment · 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

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

21 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

Related Questions

How to manipulate time in unity? 1 Answer

timer not ticking down 2 Answers

How to give points after certain number of seconds?,How to give points after a certain number of seconds? 1 Answer

Timer jitters? SOLVED 0 Answers

How do I create a mission timer? 2 Answers


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