x


Using Pause and sound on/off in Toggle

I have this code and would like to add an audio on/off and pause to it but i keep getting errors, please advice.

@script ExecuteInEditMode()

private var toggleState1 : boolean = false;

private var toggleState2 : boolean = false;

function OnGUI() {

toggleState1 = GUI.Toggle(Rect((Screen.width - 800)/2, 20, 150, 20), toggleState1, "Sound on/off");

toggleState2 = GUI.Toggle(Rect((Screen.width - 800)/2, 40, 150, 20), toggleState2, "Pause");

}

more ▼

asked Jan 15 '12 at 04:08 PM

malcolmtwl gravatar image

malcolmtwl
26 2 3 4

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

2 answers: sort voted first

Use AudioListener.pause to pause all sounds in your game - audio.mute only mutes the AudioSource of the script owner object.

@script ExecuteInEditMode()

private var toggleState1 : boolean = false;
private var toggleState2 : boolean = false;

function OnGUI() {
  toggleState1 = GUI.Toggle(Rect((Screen.width - 800)/2, 20, 150, 20), toggleState1, "Sound on/off");
  AudioListener.pause = toggleState1;
  toggleState2 = GUI.Toggle(Rect((Screen.width - 800)/2, 40, 150, 20), toggleState2, "Pause");
  Time.timeScale = toggleState2? 0 : 1;
}
more ▼

answered Jan 16 '12 at 02:26 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

It works now thanks (:

Jan 16 '12 at 02:41 AM malcolmtwl
(comments are locked)
10|3000 characters needed characters left

this is my pause button code

if(Input.GetButtonUp("Pause")){
   if(Time.timeScale > 0)
    Time.timeScale = 0;
   else
    Time.timeScale = 1;
   }

and you can use audio.mute = true/false to toggle music

more ▼

answered Jan 15 '12 at 07:28 PM

dorpeleg gravatar image

dorpeleg
864 7 13 23

ok will try it out

Jan 16 '12 at 01:46 AM malcolmtwl
(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:

x1030
x262
x162

asked: Jan 15 '12 at 04:08 PM

Seen: 891 times

Last Updated: Jan 16 '12 at 02:41 AM