- Home /
Music repeats when going back to mainmenu/scene 0
I have been using different scripts to make the bg music on selected scenes from answers.unity then i got a UnityJavaScript here https://answers.unity.com/questions/413203/continuous-music-across-multiple-scenes.html#sidr It says that it will play a different track for each level you set it to, or just tell it to stop playing for that scene but I only have to play the background music. It also makes sound stop in the game scene (scene2) and it goes well when going to other scenes (1-4) but whenever i go back to scene 0 which is the main menu the music repeats. It seems that it is also quite outdated but I do not know how to fix it since I am just self-studying. Here is the script
pragma strict
public class MusicSingleton extends MonoBehaviour { public static var instance : MusicSingleton;
public var musicMainMenu : AudioClip;
public var musicLevel1 : AudioClip;
function Awake()
{
if ( instance != null && instance != this )
{
Destroy( this.gameObject );
return;
}
else
{
instance = this;
}
DontDestroyOnLoad( this.gameObject );
}
function OnLevelWasLoaded( level : int )
{
if ( level == 0 )
{
audio.Stop();
audio.clip = musicMainMenu;
audio.Play();
}
else if ( level == 1 )
{
audio.Stop();
audio.clip = musicLevel1;
audio.Play();
}
else if ( level == 2 )
{
audio.Stop();
}
}
public static function GetInstance() : MusicSingleton
{
return instance;
}
function Update()
{
//
}
}
Any help would be much appreciated.
Your answer
Follow this Question
Related Questions
Best Unity Practices 1 Answer
audio variable for javascript 0 Answers
Need help with OnTriggerEnter 3 Answers
Using Time with OnGUI Help 2 Answers