Audio Play issue Laser Defender

Hi, I was doing the audio player as in the video, but I have a issue, when Unity compile the code, it doesn’t show any problem, but when I run the game the next message is shown, even though the music for each Scene works, this only happen on the Start Menu. I’m using unity 2017.

.
this is the code:

Down below you have solution.
First problem was fixed by adding ‘if… else’ in ‘Awake()’ method.
Second problem was fixed by changing:

// Audio source.
private AudioSource audio_source;

To:

// Audio source.
private static AudioSource audio_source;

Second problem was becouse on entire loop of scenes, when first scene(start scene) is loaded second time, method ‘OnLevelWasLoaded()’ can be run from new instance of ‘MusciPlayer’.

Hope this will help you. For me it solved the problem.

Solution

Alternative you can change ‘OnLevelWasLoaded’ to this:

// Event - when level was loaded.
public void OnLevelWasLoaded(int level)
{
// If there is instance of game music and it is not this one.
if((instance!=null)&&(instance!=this))
{
// Exit from function
return;
}
// rest of the function
// …
} // End of OnLevelWasLoaded

Personally i think that should be putted in the subtitles of the video.

Privacy & Terms