- Home /
Question by
Razputin · Jan 16, 2015 at 05:46 PM ·
destroyaudioclipinstancedontdestroyonloadaudio source
How to stop duplicates from dontdestroyonload
I've already found some solutions online for this, but they don't seem to work for audioclips. Heres what I'm trying to do.
private static AudioClip instance;
public static AudioClip Instance
{
get { return instance; }
}
private void Awake()
{
// If no Player ever existed, we are it.
if (instance == null)
instance = this;
// If one already exist, it's because it came from another level.
else if (instance != this)
{
Destroy(gameObject);
return;
}
}
}
this is my first time adding audio, so i'm having a hard time.
Comment
Best Answer
Answer by RedHedZed · Jan 16, 2015 at 06:54 PM
I did this by having a prefab whose sole purpose was to play music. All it had was an audio source on it with the background music I was playing. Then in a sort of "manager" script, I checked to see if the object existed. If it didn't, instantiate the prefab and tell it not to ever be destroyed. I didn't actually place the music in the hierarchy.