Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GaventGames · Apr 08, 2019 at 03:03 PM · scripting problemtimertimescaletimers

Is it possible to make a timer that ignores timescale?

My game sets the timeScale to 0 in between waves so that the player can upgrade weapons and barriers in menus without the next wave starting. However, I also have background music playing that has an intro. I created a second audio clip that does not have an intro which i have set up to start once the timer reaches a certain time. The menu, and subsequently the timeScale freeze, are effecting the timer that tells the audio manager when to play the second clip. Is there a way to make this timer work independently of the timeScale?

 public float timer;
     public Sound[] sounds;
     // Start is called before the first frame update
     void Awake()
     {
         foreach (Sound s in sounds)
         {
             s.source = gameObject.AddComponent<AudioSource>();
             s.source.clip = s.clip;
 
             s.source.volume = s.volume;
             s.source.pitch = s.pitch;
             s.source.loop = s.loop;
 
             
         }
     }
 
     void Start()
     {
         Play("Theme");
         timer = 0f;
     }
 
     public void Update()
     {
         timer += Time.deltaTime;
 
         if (timer >= 240f)
         {
             Play("Theme 2");
         }
     }
 
     public void Play (string name)
     {
         Sound s = Array.Find(sounds, sound => sound.name == name);
         if (s == null)
         {
             Debug.LogWarning("Sound" + name + "not found!");
             return;
         }
         s.source.Play();
     }

This is the code for reference.

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 xxmariofer · Apr 08, 2019 at 03:10 PM 0
Share

have you tried using an async method? something like

 async void StartTimer()
 {
     Debug.Log("Waiting 240 second...");
     await Task.Delay(TimeSpan.FromSeconds(240));
     Play("Theme 2");
 }
avatar image RobAnthem · Apr 08, 2019 at 03:37 PM 0
Share

Have you looked into the System.Timer class? I use it whenever I need a timer that doesn't rely on Unity, plus its super accurate because timers are threaded. You can also tie a callback into it like xxmariofer suggested.

EDIT: Sorry I forgot, System.Timers.Timer is just a timer, which while it's useful, if you want something that runs its own update, you'll want System.Threading.Timer that's the one that allows callbacks.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by unity_J016ZU_VZQzYAg · Apr 08, 2019 at 03:06 PM

Would Time.unscaledDeltaTime work for you ?
Unlike deltaTime this value is not affected by timeScale.

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 GaventGames · Apr 08, 2019 at 03:07 PM 0
Share

I'll give it a try and see if that works.

avatar image GaventGames · Apr 08, 2019 at 03:20 PM 0
Share

it definitely made the timer work. I appreciate it!

The code I provided makes the second clip play every frame creating this glitch stutter sound. i swapped out the >= for a == and it seems to not try to play it at all. Any suggestions?

avatar image unity_J016ZU_VZQzYAg GaventGames · Apr 08, 2019 at 03:25 PM 1
Share

Switch back to >= and set a boolean after playing the sound for the first time and return after every other frame if the sound is already playing (boolean true)

avatar image GaventGames unity_J016ZU_VZQzYAg · Apr 09, 2019 at 03:16 PM 0
Share

That definitely fixed the problem. Just had to play around with the loop point until it transitioned seamlessly. Thank you so much for your help! You rock!

avatar image
0

Answer by coolamigo · Apr 11, 2020 at 01:49 PM

Another option that many might find useful is WaitForSecondsRealtime. Please check that out

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 Ryox92 · Jun 24, 2020 at 06:45 AM 0
Share

I just logged in to say Thank You!!!

I was trying to figure out how to get a coroutine to work with timescale.

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

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

Related Questions

Working with Time in Unity 0 Answers

How to make a stopwatch that restarts every time the player dies and saves it as a highscore 3 Answers

Timescale issues 0 Answers

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

Countdown Timer 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