Im getting some strange behavior on my music player and I can’t seem to fix it myself.
So;
For some reason the volume on the start scene is twice as loud as any other scene. I think that may be to having two pieces of music playing at the same time. The second piece disappears when entering a different scene. There is only ever 1 music player in the hierarchy.
Whenever I loop round to the start scene, a new piece of music plays over the already persisting one, yet there is only ever one musicPlayer object in the hierarchy. My code for the MusicPlayer is identical to Bens.
If a video of the problem would help just say.
I backtracked to lesson 65 and used the following code. In addition, I decided to add a different audio file and replace the Music Player audio file source with the new file. The result was both audio files played @!#$. I then deleted the original file (Nordens…) and then it worked fine. I am not sure why this occurred, however for some reason the audio was persisting.
using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
static MusicPlayer instance = null;
// Use this for initialization
void Start () {
if (instance != null) {
Destroy (gameObject);
print ("Duplicate music player self-destructing!");
} else {
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
// Update is called once per frame
void Update () {
}
Hey Guys yeah I am having the same issue using a different audio file then Ben’s example.
Here is my code it builds fine in monodevelope but when I run and switch levels I still get the media player issue. Another odd thing is the debug command is not writing to the console to identify if the Media players are running
Using Unity 4.6.9
using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
static MusicPlayer instance = null;
void Awake () {
Debug.Log ("Music player Awake" + GetInstanceID ());
if (instance != null) {
Destroy (gameObject);
print ("Duplicate music player self-destructing!");
} else {
instance = this;
GameObject.DontDestroyOnLoad (gameObject);
}
}
void Start () {
Debug.Log ("Music player Start" + GetInstanceID ());
}
// Update is called once per frame STATICS
void Update () {
}
}
Code looks exactly as EliteStomper’s (and as shown in the tutorial) and the song just continues to double up when returning to the Start Menu.
I have tried recreating the whole Music Player from scratch, tried the boolean version, tried it in Start and Awake (yes, saving everything), and it never behaves any differently.
My logs actually do state that the duplicate is Destroyed, but it isn’t. There’s no indication of the original ID changing, or another one being created, but it does not do the thing. Here’s the debug log example from a duplicated MusicPlayer ID (11888) that was created and supposedly destroyed:
Duplicate music player detected. Self destruct 11888
UnityEngine.Debug:Log(Object)
MusicPlayer:Awake() (at Assets/Scripts/MusicPlayer.cs:11)
Could you zip up your project and provide me with a link please. If the zip file is less than 10mb you can upload it directly in to a reply to this topic. If it is larger than that, please use a service such as Google Drive or DropBox to share the zip file.
I really am starting to think it’s an issue local to my system. Right now, I have no Music Player script, no music file in assets, or in the heirarchy, and no call to audio files at all, yet my start menu still plays the music, but no other level does. I kinda feel fine with that, for the moment, and have continued on with the course. I’m guessing it’s something bungling up on my end. Thank you for the response, though, Rob! When I get to adding sound effects I’ll see how it behaves and potentially hit you up.
I am having the same issue in Unity 2017. I was thinking it was a 5.5+ issue until I saw this post. the extra volume definitely seems to be due to a second simultaneous audio signal. I tried disabling 'Play on Awake" for the audio source and found the music played at normal level but goes silent when switching scenes. I’m thinking one solution could be to leave ‘play on awake’ unchecked and to play the sound via script.
so… It appears I had a second audio source attached to my canvas. Not sure how that happened .
I would suggest you thoroughly check all the components of all your scene objects
Hey all, yeah, I checked around and didn’t find any extra audio sources anywhere, but much like I figured initially, it was some weirdness occurring locally with my machine. After a reboot, I started the whole audio lesson over again and it worked a charm. Thanks for the feedback and suggestions!