Hi everybody. I’m using unity 2017.3.1f1. The problem is there is no music at all. I think that is because the OnSceneLoaded method is not executed. But i don’t know why.
My scrip is the next:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MusicController : MonoBehaviour
{
static MusicController instance = null;
public AudioClip startClip;
public AudioClip gameClip;
public AudioClip endClip;
private static AudioSource music;
void Awake()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
}
else
{
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
music = GetComponent<AudioSource>();
SceneManager.sceneLoaded += OnSceneLoaded;
}
}
void OnSceneLoaded(Scene scene, LoadSceneMode loadscenemode)
{
//music.Stop ();
if (scene.buildIndex == 0)
{
music.clip = startClip;
}
if (scene.buildIndex == 1)
{
music.clip = gameClip;
}
if (scene.buildIndex == 2)
{
music.clip = endClip;
}
music.loop = true;
music.Play();
}
}