x


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.

more ▼

asked Mar 17 '12 at 12:45 AM

jackpile gravatar image

jackpile
61 9 12 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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
  }
}
more ▼

answered Mar 17 '12 at 02:59 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

Thanks! Sorry for replying so late! I have to get in the habit of trying small examples to prove/disprove. My 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.

Apr 18 '12 at 10:47 PM jackpile
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x588
x230
x176
x86
x18

asked: Mar 17 '12 at 12:45 AM

Seen: 979 times

Last Updated: Apr 18 '12 at 10:48 PM