- Home /
Mute Button
Hello.
I am exporting to Flash and as a result you can't edit sound in script. So to work round this you have to turn an object with the sounds on it on and off rather than simply telling the volume to be 0 in script. So i have everything working in the below script. The issue i am having is that it doesn't remember that it's muted on the next level. Here is the code i am using. This code is on an empty game object called musicManager that turns a child object of the camera called music on and off depending on whether you have clicked the mute botton or not.
var soundOn : boolean = true;
var music : GameObject;
var soundOnImg : Texture2D;
var soundOffImg : Texture2D;
function Awake () {
DontDestroyOnLoad(this);
if(soundOn == true) {
music.active = true;
}
}
function OnGUI () {
if(soundOn == true) {
if(GUI.Button(Rect(50, 0, 20, 20), soundOnImg)) {
soundOn = false;
music.active = false;
//GUI.DrawTexture(Rect(50, 0, 20, 20), soundOnImg);
}
}
if(soundOn == false) {
if(GUI.Button(Rect(50,0,20,20), soundOffImg)) {
soundOn = true;
music.active = true;
}
}
}
So i have a dont destroy on load and the same set up of objects in the second level. If i don't have the if sound is true in the awake then when you start the game there is no sound playing until you click the button to the muted state and then again to play the sound. The issue is that when you start the next level this awake function is called again and therefore the music gameobject is set to true again. I realise this is hard to follow so if there is something you don't understand let me know.
Basically... I need music to be set to active on awake but then for it not to call that awake function again in the next level...
Answer by TheFrankman123 · Apr 24, 2012 at 03:33 PM
Forget it, i was being dumb. During part of my testing i had disabled the music game object in the inspector, so when you started the game it wasn't playing until you clicked mute on the off again. So the final code is very much what is set as above in case it is useful to anyone but remember. THIS IS A VERY SHIT WAY TO DO IT. I am only doing it this way because Flash Export doesn't support editing sound in script.
var soundOn : boolean = true;
var music : GameObject;
var soundOnImg : Texture2D;
var soundOffImg : Texture2D;
function Awake () {
DontDestroyOnLoad(this);
}
function Update () {
if(soundOn == true) {
Debug.Log("sound is true");
}
if(soundOn == false) {
Debug.Log("sound is false");
}
}
function OnGUI () {
if(soundOn == true) {
if(GUI.Button(Rect(50, 0, 20, 20), soundOnImg)) {
soundOn = false;
music.active = false;
}
}
if(soundOn == false) {
if(GUI.Button(Rect(50,0,20,20), soundOffImg)) {
soundOn = true;
music.active = true;
}
}
}
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad level mute. 0 Answers
how do i mute all audio sound? 3 Answers
mute/un-mute 1 Answer
Mute/Unmute not working with new sounds 2 Answers
Mute/Unmute Sound For All Game Scenes by pressing Button 1 Answer