- Home /
AudioSources don't play when setting Time.timescale to 0
I'm using the standard trick of setting Time.timescale = 0 to pause my game. Then I present buttons when paused to restart the level, go to the main menu, mute, shop, etc. I want to play a consistent button sound across all UI buttons when clicked, but when in game pause, my button clip doesn't play immediately. I assume that Time.timescale=0 tells Unity to also pause new audiosources. Is there any way around this and to make them play immediately? I can't find anything on the forum or in documentation. Thanks.
Answer by coah · Aug 11, 2013 at 08:59 PM
I got this problem as well today. I found a silly solution.
Just do this;
Time.timeScale = 1;
audio.PlayClipAtPoint(sfx.soundTest, Camera.main.transform.position, 1.0 );
Time.timeScale = 0;
The "One Shot Audio" objects accumulate in the hierarchy while the timeScale is 0, but they all go away once the timescale is 1 for a full frame again.
Answer by aldonaletto · Mar 17, 2012 at 02:59 AM
AudioSources play independently of Time.timeScale - probably the code that should call Play() is being stopped when timeScale becomes 0. If you use the code below, the sound assigned to audio.clip will be played anytime the button pause is clicked (attach this code to the object that has the AudioSource):
private var pause = false; private var lastValue = false;
function OnGUI(){ pause = GUI.Toggle(Rect(10,10,200,60), pause,'Pause'); if (pause) Time.timeScale = 0.0; else Time.timeScale = 1.0; if (pause != lastValue){ // if pause changed... lastValue = pause; audio.Play(); // play the sound } }
Thanks! Sorry for replying so late! I have to get in the habit of trying small examples to prove/disprove. $$anonymous$$y bad. I'm too focused on just getting my app out the door after many months. I don't think my code is getting paused as my callbacks from buttons are being issued (EZGUI) and my calls to Play() are being made. Something else must be going on. Cheers.
$$anonymous$$y experience has been that AudioSources that already exist play independent of TimeScale. If you're using PlayClipAtPoint() you'll need to toggle the timescale as coah described above, or create the AudioSource component before you set TimeScale to 0.
Answer by Dannabe1 · Nov 11, 2020 at 09:45 AM
Don't use audioSource.PlayOneShot(audioClip), it will wait until Time.timeScale will be greater than 0. Use audioSource.Play() asigning before audioClip to AudioSource.
Sometimes, people use Time.timeScale = 0f and AudioListener.pause = true or AudioListener.volume = 0. In these cases, you must set these AudioSource properties to true:
audioSource.ignoreListenerPause = true;
audioSource.ignoreListenerVolume = true;