- Home /
Prevent audio listener from duplicating
I have made a script, which makes a game object not destroy if a boolean is true or destroys if the boolean is false. Then on this game object I have my "menu song" playing, then when I load a "game scene" I have gotten another script which turns the boolean to false so that the "menu music" isn't playing. Now my problem is that, I need to have the audio listener on the game object with the audio in it to here the music, but then because of the "DontDestroyOnLoad" the audio listener duplicates every time I go from "second menu scene" to the "first menu scene". How would I prevent the audio listener from duplicating?
Here is my first script.
static var MenuMusicPlaying : boolean = true;
function Awake() {
if (MenuMusicPlaying == true) {
DontDestroyOnLoad(gameObject);
} else if (MenuMusicPlaying == false) {
Destroy(gameObject);
Debug.Log("MenuMusicDestroyed");
}
}
Here is my second script.
var targetScript : MenuMusicDontDestroy;
function Awake () {
targetScript.MenuMusicPlaying = false;
}
Answer by ThePunisher · May 05, 2014 at 05:21 PM
This is easily solved by having an "initialize" scene that has a all your initialization scripts and things like your main camera that will persist throughout the entire game and usually have DontDestroyOnLoad set. Then a simple script in that scene can just transfer you to the main menu or whatever your first actual scene will be.
Right, DontDestroyOnLoad should only be used in a scene that is loaded only once.
Thanks for adding that, Bunny83. I forgot to mention it.
Thanks for your response it lead me on the right way to fix it
I use DontDestroyOnLoad on scenes that can be loaded and reloaded, I just add some checks and logic to make it happen flawlessly.
Your answer
Follow this Question
Related Questions
Prevent countdown from reloading every time a new scene is loaded 1 Answer
Time.timeScale = 0 did not prevent click 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Java Script Dont Destroy On Load Duplicates Game Object 3 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers