- Home /
I'm having problem with Continuing Menu Music
Hello everyone, I have a Main Menu music which continues through another scene (like Options) BUT it also continues to my "FirstLevel" which I really don't want it to. I've no idea how to fix this, can somebody help me? Here is the script I'm using:
private static var instance:MusicContinue;
public static function GetInstance() : MusicContinue
{
return instance;
}
function Awake() {
if (instance != null && instance != this) {
Destroy(this.gameObject);
return;
} else {
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
Answer by meat5000 · Oct 17, 2013 at 01:40 PM
Take this out
DontDestroyOnLoad(this.gameObject);
Or any DontDestroy you have on your music objects.
Also, consider stopping the music...
[http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.Stop.html][1]
for use with an AudioSource component.
Perhaps try
[http://docs.unity3d.com/Documentation/ScriptReference/Application-loadedLevelName.html][2]
and stop the music when the particular level is loaded. [1]: http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.Stop.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/Application-loadedLevelName.html
Hello, when I took
DontDestroyOnLoad(this.gameObject);
out, music continuing through my Options scene was gone, it starts it all over again. But the problem was solved as when in FirstLevel, music cut out.
What I only want is to make music stop when I click New Game (which takes players to FirstLevel), but not in Options
Also, couldn't make AudioSource.Stop work. Tried creating an empty object and getting the script in it. I'm not so very good at this, you see :/
Can we use/make something like if(NewGameButton) destroy on load, to not make the music continue only when we get in FirstLevel?
Answer by ThePositiveMoxie · Oct 17, 2013 at 02:42 PM
AudioSource.Stop() should only be called on the NewGame button statement for when clicked. DontDestroyOnLoad() should be used in the scene you do not want it destroyed.
var audioSource : AudioSource;
if (ButtonYouArePressingForNewGame){
audioSource.Stop();
Application.LoadLevel("NewGameLevel");
}
if (ButtonPressedForOptions){
Application.LoadLevel("OptionsMenu");
}
In your Options Scene, put in Start or Awake, but DO NOT put it in the NewGame Scene.
function Awake(){
DontDestroyOnLoad(ObjectToNotDestroy);
}
I didn't full test this, but based off the documentation for DontDestroyOnLoad that is how this will work. Easy as that. Hope this is right, as I did no testing.
Thank you so much for help, but there is one last left (I thought to be very simple one, but I didn't understand your last part so it's probably because of that since it says "AudioSource" destroyed but you're still trying to reach it.)
Anyways, here's my last problem with this (didn't write the last function Awake(){ DontDestroyOnLoad(ObjectToNotDestroy); }
party because I've no idea where to write it to)
When I'm in $$anonymous$$ain$$anonymous$$enu, and get in Options. $$anonymous$$usic continues which is good. When I'm in $$anonymous$$ain$$anonymous$$enu, and get in FirstLevel, $$anonymous$$usic stops which is good
BUT
When I'm in $$anonymous$$ain$$anonymous$$enu, and get in Options, then go back to $$anonymous$$ain$$anonymous$$enu and click New Game should load FirstLevel but nothing happens. And some error in bottom says:
$$anonymous$$issingReferenceException: The object of type 'AudioSource' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. $$anonymous$$ain$$anonymous$$enu.On$$anonymous$$ouseUp () (at Assets/Copycat/Scripts/$$anonymous$$ain$$anonymous$$enu.js:35) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
Can someone help?
Easy fix... include the same DontDestroyOnLoad in Awake in your main menu as well. So it carries over between options, then back. Not including it in the main menu will destroy the one that's in options. So it no longer exists. That is why you get the error.
This needs to be included in any kind of script you want. As long as you place it in start or awake functions and are calling to the right AudioSource's object.
Also you won't be able to goto $$anonymous$$ain$$anonymous$$enu only because of the error. Script won't run through completely because it found an error, object destroyed. So it doesn't know what to do. Once that's fixed you will be able to freely go into New Game again.