[SOLVED] My script works only for the Splash Scene and does not launch any music on the subsequent Scene

Hi everyone,

I am using Unity 2017 and I am attempting not to use LoadLevel and OnLevelWasLoaded because they are marked as obsolete. I manged to create my own Level Manager game objects and scripts with the usage of SceneManager.

I am trying to create my Music Manager/Music Payer script using SceneManager as well. So far I managed to get it working for the Splash Scene, but whenever I move to Main Menu scene there is no music (even though the logs correctly state the number of a Scene after load).

I know that the easiest way to make it work would be to manually assign AudioClips and use ‘if’, but I would rather have a flexible script that could be used throughout my future project with ease.

Here is the code that I have so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MusicPlayerScript : MonoBehaviour {

public AudioClip[] MusicPerSceneArray;
private AudioSource audioSource;
private int NewMusicSceneNumber;

private void OnEnable ()
    NewMusicSceneNumber = SceneManager.GetActiveScene().buildIndex;
    Debug.Log("SplashSceneNumber: " + NewMusicSceneNumber);
    audioSource = GetComponent<AudioSource>();
    SceneManager.sceneLoaded += NewSceneLoaded;


void NewSceneLoaded(Scene scene, LoadSceneMode mode)
    NewMusicSceneNumber = scene.buildIndex;
    Debug.Log("NewMusicSceneNumber: " + NewMusicSceneNumber);
    mode = LoadSceneMode.Single;

private void ChangeMusicBasedOnSceneNumberAndArray()
    if (NewMusicSceneNumber == 0)
        audioSource.loop = false;
        audioSource.loop = true;

private void PlayMethod()
    audioSource.clip = MusicPerSceneArray[NewMusicSceneNumber];


I would appreciate any help from you guys. It has been hours now and I have no solution to my problem.


Privacy & Terms