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
2
Question by RafaelVazquez · Mar 20, 2012 at 11:48 PM · timerreloadtime.time

Time.time stops working after reloading a level

Hi everybody, I've been having problems with this for quite some time, and I can't get my head around it. The basic idea is quite simple: have a countdown timer at the beignning of each level. It works quite well, and even allows you to pause the game during the countdown and it will respect that. The problem I'm having is after reloading the scene. The first time it runs, it works perfectly, however the second time, Time.time stops counting. I've printed the timer on console and what it shows me is that Time.time simply stops after laoding the scene. I've tried using fixedTime, timeSinceLevelLoad and the regular time.

Interestingly, it starts working when I use realTimeSinceStartUp. However, this causes other isues (such as beign unable to pause the game on countdown).

Does anyone know why time.time stops counting after reloading the scene?

Here is my code:

   using UnityEngine;
     using System.Collections;
     
     public class GamePrep : MonoBehaviour {
         
         public bool useTimer;
         public int countdownTime;
         
         //the time when the script is called
         private float startingTime;
         
         //a buffer variable for time operations
         private float guiTime;
             
         //the number you will display
         private int displaySeconds;
 
         
         // Use this for initialization
         void Start () {
             
             
             #region Timer initialization
             //if we are in prep mode and are going to use the countdown time
             if(useTimer){
                 
                 //initialize seconds
                 displaySeconds = countdownTime;
                 
                 //save the time
                 startingTime = Time.time;    
     
             }//end if
             #endregion         
             
         }//end start
         
         void OnGUI(){
             
             guiTime = Time.time - startingTime;
                              
             print(Time.time + " : "+ startingTime);
             
                 #region Timer    
                 //start the timer
                 if(useTimer )
                 {
                 
                     displaySeconds = Mathf.CeilToInt( countdownTime - guiTime);
     
                 }//end if
                 #endregion    
 
                 //if the timer has reached 0
                 if(displaySeconds <= 0){
                     
                     //when the countdown stops; change to play mode
                     GameMode.currentMode = GameMode.gameMode.play1;
                 
                 }

         }//end mehtod
         
     }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by RafaelVazquez · Mar 21, 2012 at 03:19 AM

I moved the time keeping to an update method, but to no avail. I was thinking it might have something to do with the timeScale. See, restarting the level comes from the pause menu, which sets the timeScale to 0. Is it possible that after loading the scene, the timeScale is not reset to 1?,Thanks for the answer. I moved everything to the update, however it still behaves the same way. A thought occured to me, but I haven't been able to test it. In order to reload the level, I first have to pause the game, which sets the timeScale to 0. Can it be possible that the timeScale is not resetting to 1 after reloading the scene?

Comment
Add comment · Show 2 · 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 venkspower · Mar 21, 2012 at 05:58 AM 0
Share

Please, post your comment in the comment section. That is why they are there for. Don't post your query as the answer!

avatar image Combinacijusx · Jul 11, 2015 at 01:01 PM 0
Share

Thanks set my Time.timeScale = 1 in Start() and now everything works :)

avatar image
0

Answer by venkspower · Mar 21, 2012 at 05:56 AM

I think you should reset the time in the beginning of each level, and call the function wherever necessary.

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
avatar image
0

Answer by DaveA · Mar 20, 2012 at 11:51 PM

I'm not sure about using Time in OnGUI, and putting print in there, oooh. That gets called several times per frame you know. You can move the Time keeping to Update and use just those variables you set in OnGUI, and use a GUI.Label to print Time.time if you need to. That said, I don't know why it would behave that way, and differently on different runs.

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
avatar image
0

Answer by RafaelVazquez · Mar 21, 2012 at 05:49 AM

I managed to make some tests on that last idea and its now definitly working. Thank you for your attention.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause Time.time? 1 Answer

Why is the timer increasing after every reset? 3 Answers

Reload level after timer hits 0 1 Answer

How to reset Time.time ? 1 Answer

Time.deltaTime stops counting up, after the Player is destroyed 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