- Home /
How to make music play always without duplicates?
I have some problem. I wanted to make my audio clip play always between the scenes. So i created a Gameobject, atached a audio sorce, and script public bool Always; public GameObject music;
void Awake ()
{
if (Always)
{
DontDestroyOnLoad(music.transform.gameObject);
}
}
It works good, but.... The game starts from first scene, then if i move to another scene music is playing. But when i go back to first scene , my gameobject is still not destroyed and music is playing.But it appears a new the same gameobject with this audiosource and there are two audioclips playing at the same moment. Every time when i am going back to first scene new gameobject with audiosource appears and starts playing.
How can i avoid this? Thanks
Put a check on the creation of your first music object that one doesn't already exist or the music isnt playing.
The key is in the object itself that contains the music.
Find the place that the object is created or the music is setup for the first time. Place an if check on it.
Alternatively, gameobject find all the music objects and if more than one if found, delete the duplicates.
Also look on the learn site for data persistance. It covers this exact check in that video. Around 15 $$anonymous$$utes in.
Answer by jarado9 · Feb 24, 2015 at 07:20 PM
public class MusicPlay : MonoBehaviour
{
public static MusicPlay musicplay;
public bool Always;
public Gameobject music;
void Awake()
if (musicplay == null)
{
DontDestroyOnLoad(gameObject);
musicplay = this;
}
else if (musicplay != this)
{
Destroy(gameObject);
}
}
}
sorry I can only answer once, so had to delete my old answer, and I lost the code you last submitted. Still need know why you need it to be a bool and where you set the true/false value of the bool. else it's hard to help/ understand
The answer I just gave is all I can offer, and I suggest if this doesn't work, to post the code you posted before that was in the answer I deleted, and some detail of where the Always bool is being set to true or false. I will leave it for someone else to answer now, to avoid confusing of deleting my answer again. Sorry if I haven't helped.
I will try your last code and will see. i just didn't understand when you told to make script static.
I didn't understand why you have this Always bool because nowhere can see when you give it a true/false value, all I can think is you just tick it as true in the inspector. Hope you sorted it out now, and making it public static is so other scripts can access it from scene to scene, hence and can delete duplicates of itself. sorry for the confusion it was difficult to explain public static until you posted top part of your code as well, so I could see the script name and your declared variables. If my answer has sorted your problem, please accept answer, else I suggest re-paste your code here for someone to answer. or watch http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading which is pretty much where my answer is from, and this training video is very good for explaining keeping data & gameobject from scene to scene, and deleting duplicates.
Answer by siaran · Feb 25, 2015 at 12:07 PM
I think the best way here is to make your music container a singleton.
See here for an explanation on singleton patterns in Unity: http://unitypatterns.com/singletons/
Your answer
Follow this Question
Related Questions
Specifying OnAudioFilterRead()'s audiosource 1 Answer
GetSpectrumData over entire file 1 Answer
Glitched Music 0 Answers
PlayScheduled or PlayDelayed 0 Answers
Stereo Mix as Input? 1 Answer