Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by BravoJWhiskey · Aug 22, 2015 at 07:17 AM · timetimerpausetimescale

Problem setting Time.timeScale to 1 after being set to 0

So I've just made a script that will pause the game and bring up a window with three buttons (main menu, retry and continue) once the player has completed the level or failed it. This is what it looks like, except when the player fails (run out of time), it says "You ran out of time!". Apart from that there is no other difference. All the buttons function the same regardless. alt text

When the player completes the level or fails it, Time.timeScale is set to 0. This is when the window comes up and the player can click on one of the buttons (what they do is self-explanatory). Now if the player completes the level and then chooses one of these options, Time.timeScale is set to 1 and will load the scene, and everything works perfectly. However, if the player runs out of time and fails, when the player chooses to go to the Main Menu, retry or continue, Time.timeScale doesn't seem to set back to 1 and the game is still paused but the correct scene is loaded so I know the function is being called. I know it's something to do with the timer as I've called the same Fail function I created using OnCollisionEnter (so when the player collided with a certain object they fail), and everything worked as it should. I'm not a programmer so maybe the answer is completely obvious but here is the script:

     private int levelNumber;
 
 
     public Canvas endLevelCanvas;
     public Text outcomeText;
 
 
     public Text timerText;
     public float startTime;
     private string currentTime;
     
     // Use this for initialization
     void Start () {
         levelNumber = Application.loadedLevel;
 
         endLevelCanvas.enabled = false;
         
     }
     
     // Update is called once per frame
     void Update () {
         
         startTime -= Time.deltaTime;
         currentTime = string.Format ("{0:0.0}", startTime);
         timerText.text = currentTime;
         if (startTime <= 5) {
             timerText.color = Color.red;
         }
         if (startTime <= 0) {
             FailLevel ();
         }
 
         if (Input.GetKey (KeyCode.Escape)) {
             QuitToMenu ();
         }
     }
 
     public void FailLevel () {
         Time.timeScale = 0;
         endLevelCanvas.enabled = true;
         outcomeText.text = "You ran out of time!";
     }
 
     public void WinLevel () {
         Time.timeScale = 0;
         endLevelCanvas.enabled = true;
         outcomeText.text = "Level Completed!";
     }
 
     public void QuitToMenu () {
         Application.LoadLevel (0);
         Time.timeScale = 1;
     }
 
     public void ContinueToNextLevel () {
         Time.timeScale = 1;
         Application.LoadLevel (levelNumber + 1);
     }
 
     public void RetryLevel () {
         Time.timeScale = 1;
         Application.LoadLevel (levelNumber);
     }
 }

Sorry if I've missed something, I'm new to Unity Answers. Any help is greatly appreciated, thanks!

1.png (40.4 kB)
Comment
Add comment · Show 7
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 Scribe · Aug 22, 2015 at 08:29 AM 0
Share

Only thing I'd be worried about is QuitTo$$anonymous$$enu, I would reorder the execution of timeScale and LoadLevel. Other than that, add some debugs to make sure you are calling these method and not some other method to load your new level.

avatar image BravoJWhiskey · Aug 22, 2015 at 12:48 PM 0
Share

Ah I forgot to change that one, originally they all executed LoadLevel first but I changed them around to see if it did anything, unfortunately that wasn't it :( I've already tried adding debugs to make sure they were working and it appeared they were still being called so I'm not too sure what's going on there. Thanks for the reply anyway!

avatar image Scribe · Aug 22, 2015 at 03:21 PM 0
Share

Does it happen for every level load? or just a specific level, find all places where you change timeScale and put a debug before, maybe something else is being called on the start of the new level?

avatar image Scribe · Aug 22, 2015 at 04:16 PM 0
Share

This may be a bug, people had a similar issue here, though it was quite a while ago. I would suggest either delaying the LoadLevel call by 1 frame so the timeScale is properly reset first, or alternatively, set the timeScale to 1 on the start or awake of each level ins$$anonymous$$d.

avatar image BravoJWhiskey · Aug 22, 2015 at 10:10 PM 0
Share

Every scene that can be loaded from that window (main menu scene, current scene and the next scene) have this problem (only when the player fails though. If the player completes it, everything works fine). Thanks for the link, I'll try delaying or setting the timeScale at the start of each level, see how I go. Thanks a lot, appreciate it!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

why doesn't timescale work? 0 Answers

how can i take time value from timer (Asked again clearly) 2 Answers

Input System no button press when timescale = 0 1 Answer

Load scene after time 1 Answer

When I restart the game, it doesn't return to the start time I wanted 0 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