- Home /
DontDestroyOnLoad level mute.
I recently posted a similar question but i thought i had fixed it and i hadn't.
The issue i am having is that i need to turn my back ground music off when someone clicks the mute button. Unfortunately because i am exporting to flash i can't just tell the volume to be 0. So, on my main camera i have a game object called music which in script i am getting to turn off and turn on when the mute button is clicked. The music is declared as a public variable but the issue is that when you change level it loses music game object. So what i need to do is either define music as a private variable and find my name or find by tag. That way when it brings the soundManger GameObject over that has the script attached it will be able to find the music game object in the new scene and apply the same stuff to it. I tried doing it in the following way, however when do this it throws up the following error:
Assets/soundOnOff.js(15,32): BCE0077: It is not possible to invoke an expression of type 'String'.
What do I do!?
#pragma strict
var soundOn : boolean = true;
private var music : GameObject;
var soundOnImg : Texture2D;
var soundOffImg : Texture2D;
function Awake () {
DontDestroyOnLoad(this);
}
function Update () {
var music = gameObject.name("music");
if(soundOn == true) {
music.active = true;
} else if (soundOn == false) {
music.active = false;
}
}
function OnGUI () {
if(soundOn == true) {
if(GUI.Button(Rect(570, 0, 30, 30), soundOnImg)) {
soundOn = false;
}
}
if(soundOn == false) {
if(GUI.Button(Rect(570,0,30,30), soundOffImg)) {
soundOn = true;
}
}
}
I did gameObject.Find and that solved that error. However, it then throws up that music.active = true; is a null reference exception
I think this is because once the game object isn't active anymore it can't find the object by name...
is there a way to deactivate just the audio source maybe?
If i wasn't clear before if you are exporting to Flash then you can't edit sound in script which is why i have to active and deactive a game object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070717im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Does Flash Export Support Application.LoadLevelAysnc 1 Answer
Continue playing sound effect on camera after LoadScene 1 Answer
Mute Button 1 Answer
Horrible footstep looping problem 2 Answers