- Home /
Unity mainmenu audio problem.
I'm trying to have mainmenu have music playing in background, but when I load the level, and return back to mainmenu, the music doesn't start. Also it breaks the audiosource so I have to restart Unity everytime I load back to mainmenu from other scene.
Here is the script im using to play audio:
var MenuAudio : AudioSource;
function Start() {
MenuAudio.Play();
print("Playing");
}
The video explains better than words.
Video Link
Answer by Bincredible · May 29, 2015 at 05:14 PM
What you could do is in your load level script use the BroadcastMessage() function to tell the script to play the music. In the script you have shown add the following:
PlayMusic(play : boolean){
if(play){
MenuAudio.Play();
print("Playing");
}
}
And in the load level script, right after the Application.LoadLevel() line add the following:
BroadcastMessage("PlayMusic", true);
I tried to do this, but it doesn't seem to work.
$$anonymous$$y load menu script:
function $$anonymous$$enu() {
Time.timeScale = 1;
Time.fixedDeltaTime = 0.02 * Time.timeScale;
Application.LoadLevel($$anonymous$$enuScene);
Broadcast$$anonymous$$essage("Play$$anonymous$$usic", true);
}
$$anonymous$$enu function unpauses, loads scene and broadcast's the message. But I don't see it working in console.
Broadcast$$anonymous$$essage() doesn't send a message to the console, it calls a function in this case called "Play$$anonymous$$usic" which can be in any script. So what this does is calls the script in the menu when you return to start the music again if that makes sense.
I know, I meant it doesn't print "Playing" in the console.
Your answer
Follow this Question
Related Questions
Audio problems with unity after reloading my game from the main menu 1 Answer
Everytime Unity Editor is launched. I am asked to Sign into your Unity Account 4 Answers
Editor Interface Gets Scrambled 0 Answers
OnServerStart not being called in standalone build. Works in editor. 1 Answer
Why is it taking Unity so long to process video's? 0 Answers