Question by
CrazyPulse · Dec 06, 2018 at 04:21 PM ·
scene-loadingscene-switchingdestroy objectif statement
If statement - destroy audio source on specific scene?
Intention is to. 1. Prevent duplicates incase game ever tries to run multiples of musicplayer. 2. Destroy musicplayer at the start of a specific level. The problem i'm having is that the musicplayer isn't being destroyed at the specified level ("3_4"). Any help would be greatly appreciated.
{
static MusicPlayer instance = null;
void Awake()
{
GameObject.DontDestroyOnLoad(gameObject);
if (instance != null)
{
Destroy(gameObject);
print("Duplicate MusicPlayer self destructing");
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("3_4"))
{
Destroy(gameObject);
print("MusicPlayer self destructing");
}
else
{
instance = this;
}
}
Comment
Answer by CrazyPulse · Dec 08, 2018 at 04:16 AM
I got it working with help from villevli on a similar question, so thankyou!
void Awake()
{
GameObject.DontDestroyOnLoad(gameObject);
if (instance != null)
{
Destroy(gameObject);
print("Duplicate MusicPlayer self destructing");
}
else
{
instance = this;
}
}
void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name == "3_4")
{
Destroy(gameObject);
}
if (scene.name == "Lose")
{
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Multiple Entrances to levels 0 Answers
Scene Load/Unload Fails to Reload 1 Answer
,delay after dying and going back to main menu (new scene) 0 Answers
how to store and save scene changes for loading later 0 Answers
Loading and Unloading scenes 2 Answers