Music don't reproduce!

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();
    }
}

A few things to check. Firstly is the code inside OnSceneLoaded ever running? To test this add a debug.Log at the start of the method before your if statements and check this is triggering. Also make sure you have a music player in the scene when the game is running and you’re experiencing the issue hit pause and go into scene view and look for the music player on the hierarchy.

1 Like

thank you for your answer. I used debug.Log and the method initialized correctly. The “if” statement also initialize well. The problem seems to be in the following lines.

music.clip = gameClip; music.loop = true; music.Play(); I tried to use AudioSource.PlayClipAtPoint insted. And worked but the sound doesn’t loop. The error is the next :
UnassignedReferenceException:The variable endClip of MusicController has not been assigned.

Ok so the issue is that the variable isn’t assigned. Have you dragged the audio clipa onto the AudioClip variables on all instances of the MuscController?

Yes i did :confused:

When the game is running and you hit pause and click into the scene view does the MP object have the files attached?

No, it doesn’t.

Privacy & Terms