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 Rgalaxy · Jan 22, 2014 at 12:34 AM · guitimertimescalecountdown

Time.timeScale problem

So, i creating a popup GUI that will show a countdown timer, but i need to make the scene behind the popup GUI standstill.. i've been searching awhile but i don't get anything that could satisfied my need.. here's my code for the process..

 function OnGUI() {
     if(GUI.Button(new Rect(30,350,80,80)," ",tombol1)){
         popUp = true;
         
         Attack1();
         if(enemy_hp<=0){
             enemy_hp=0;
             MonsterIsDead();
         }
         if(player_hp<=0){
             player_hp = 0;
             PlayerIsDead();
         }    
     }
     else if(GUI.Button(new Rect(90,280,80,80)," ",tombol2)){
         
     }
     
     else if(GUI.Button(new Rect(130,350,80,80)," ",tombol3)){
     
     }
     if(popUp){
             Time.timeScale = 1;
             GUI.BeginGroup(new Rect(208,97,350,350),soal);
             GUI.Label (new Rect(100,20,50,50),seconds.ToString());
             InvokeRepeating("Countdown",1.0,1.0);
             GUI.EndGroup();
             
         }
 }
 function Countdown(){
     if (--seconds == 0) CancelInvoke ("Countdown");
     seconds = parseInt(seconds.ToString());
     if(Input.GetKeyDown("a")){
         CancelInvoke("Countdown");
     }
 }

so i tried it and when i encounter the GUI, the counter does counting down, but in hyperspeed, i am not sure what happening there.. but if i set the Time.timeScale to 0, the countdown timer just not counting.. it really confuse me, what should or could i do to fix this kind of problem?

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 superluigi · Jan 22, 2014 at 01:21 AM 0
Share

What rutter said. It seems like you might be calling invokerepeating in an update function. For example in "if(pop up){}" you call for InvokeRepeating(line 26). If "if(pop up){}" is running in an Update function it wont count correctly as it'll get updated called every frame(Update gets called a ton load of times a sec, that's why your timer runs way too fast). I always put invokeRepeating outside of any update functions and it works the way I want.

avatar image Rgalaxy · Jan 24, 2014 at 04:38 AM 0
Share

@superluigi aww, didn't see your psot back then.. yeah i understand it.. but i need it to be there, is there no other way to fix it?

i want to show the timer on this GUI and counting down only when this GUI popup.. its ok if it will be an GUIText or GUI from script.. as long as it only show up when the popup comes..

i've been searching for awhile but really, i couldn't find how to do it.. i know there'll always be some way to achieve this, but maybe it's because i lack of algorithm logic that i still don't know how to do it..

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Jan 22, 2014 at 01:09 AM

I see two problems:

First, it looks like you might be calling InvokeRepeating once per frame while the popup is active. That's going to cause quite a few calls to Countdown, and that's why your timer was moving too fast. Your script assumes that each call means one second has passed; that's only true if you call it once per second.

Second, setting Time.timeScale to zero will freeze time completely. It's easy to overlook some of the implications. Among other things, coroutines and invokes that rely on conventional time passing will stop, and that's why your timer was stopping. Animations and audio that are tied to time will also stop. There are some popular workarounds which are mentioned in this thread: Freeze game using Time.timeScale = 0. Most involve some clever use of `Time.realtimeSinceStartup`, which is not affected by timeScale.

From the workarounds thread, you might want to try the very small timescale, or pause the game completely and switch to coroutines.

Comment
Add comment · Show 1 · 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 Rgalaxy · Jan 23, 2014 at 12:54 AM 0
Share

yeah, i wanted it to pause completely but the GUI timer still moving with normal condition.. but after u pointed me the problems, i'll try to see what can i do about it..

and also. Time.realtimeSinceStartup could move only the timer i made up there? or it is something that will move(counting) from Startup of the scene?

Thanks @rutter

EDIT :

ok, i figured out that i don't need time.TimeScale = 0; will there be any way to still make the time move as slow as 1 seconds? or maybe is that possible to Invoke the countdown on the outside of GUI but the text inside the GUI "changing" as the seconds?

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

20 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

Related Questions

C# countdown timer 9 Answers

How to stop a timer with GUI button? 1 Answer

Countdown Timer 1 Answer

Problems in c# countdown timer 1 Answer

3, 2, 1 GO! 3 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