ArgumentNullException: Value cannot be null, DontDestroyOnLoad problem

Hi, i’m having problem with my code. So I have 3 scene in total, MainMenu, BattleScene, and Credit. My AudioManager.cs is loading first at MainMenu and I use DontDestroyOnLoad to make sure my audio is playing in every scene. When the battlescene have been finished, it’s loading to Credit Scene and after that it’s loading again to MainMenu.

When I’m accessing my BattleScene again, my S.source on line 82 says it’s null. Here’s the code for it.

AudioManager.cs

using System;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;

public class AudioManager : MonoBehaviour
{
    public static AudioManager Instance;

    [SerializeField] private AudioMixerGroup musikMixerGroup;
    [SerializeField] private AudioMixerGroup soundEffectsMixerGroup;
    [SerializeField] private Sound[] sounds;

    private static AudioManager instance;

    private void Start()
    {
        Instance = this;

        foreach (Sound s in sounds)
        {
            s.source = gameObject.AddComponent<AudioSource>();
            s.source.clip = s.audioClip;
            s.source.loop = s.isLoop;
            s.source.volume = s.volume;

            switch (s.audioType)
            {
                case Sound.AudioTypes.soundEffect:
                    s.source.outputAudioMixerGroup = soundEffectsMixerGroup;
                    break;

                case Sound.AudioTypes.music:
                    s.source.outputAudioMixerGroup = musikMixerGroup;
                    break;
            }

            if (s.playOnAwake)
                s.source.Play();

            if (AudioManager.instance == null)
            {
                AudioManager.instance = this;
                GameObject.DontDestroyOnLoad(this.gameObject);
                SceneManager.sceneLoaded += OnSceneLoaded;
            }
            // else
            // {
            //     Destroy(this.gameObject);
            // }
        }
    }

    void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode)
    {
        if (scene.name == "Death" || scene.name == "HappyEnding")
        {
            this.gameObject.SetActive(false);
            Debug.Log("I am inside the if statement");
        }
        else
        {
            this.gameObject.SetActive(true);
        }
    }

    public void Play(string clipname)
    {
        Sound s = Array.Find(sounds, dummySound => dummySound.clipName == clipname);

        Debug.Log("Clipname: " + clipname);
        if (s == null)
        {
            Debug.LogError("Sound: " + clipname + " does NOT exist!");
            return;
        }

        // if(s.source == null)
        //     gameObject.AddComponent<AudioSource>();
        
        Debug.Log("S.source: " + s.source);
        s.source.Play();
    }

    public void Stop(string clipname)
    {
        Sound s = Array.Find(sounds, dummySound => dummySound.clipName == clipname);
        if (s == null)
        {
            Debug.LogError("Sound: " + clipname + " does NOT exist!");
            return;
        }
        s.source.Stop();
    }

    public void UpdateMixerVolume()
    {
        musikMixerGroup.audioMixer.SetFloat("Music", Mathf.Log10(AudioOptionsManager.musicVolume) * 20);
        soundEffectsMixerGroup.audioMixer.SetFloat("SFX", Mathf.Log10(AudioOptionsManager.soundEffectsVolume) * 20);
    }

    public void muteMusic(bool isMusicMute)
    {
        if (isMusicMute)
        {
            musikMixerGroup.audioMixer.SetFloat("Music", -80);
        }
        else
        {
            musikMixerGroup.audioMixer.SetFloat("Music", AudioOptionsManager.musicVolume);
        }
    }

    public void muteSFX(bool isSFXMute)
    {
        if (isSFXMute)
        {
            soundEffectsMixerGroup.audioMixer.SetFloat("SFX", -80);
        }
        else
        {
            soundEffectsMixerGroup.audioMixer.SetFloat("SFX", AudioOptionsManager.soundEffectsVolume);
        }
    }
}

and here’s my another code for audio.

AudioOptionsManager.cs

using UnityEngine;
using TMPro;

public class AudioOptionsManager : MonoBehaviour
{
    public static float musicVolume;
    public static float soundEffectsVolume;

    [SerializeField] private TextMeshProUGUI musicSliderText;
    [SerializeField] private TextMeshProUGUI soundEffectsSliderText;

    public void OnMusicSliderValueChange(float value)
    {
        musicVolume = value;
        
        musicSliderText.text = ((int)(value * 100)).ToString();
        AudioManager.Instance.UpdateMixerVolume();
    }

    public void OnSoundEffectsSliderValueChange(float value)
    {
        soundEffectsVolume = value;

        soundEffectsSliderText.text = ((int)(value * 100)).ToString();
        AudioManager.Instance.UpdateMixerVolume();
    }
}

Sound.cs

using System;
using UnityEngine;

[Serializable]
public class Sound
{
    public enum AudioTypes { soundEffect, music }
    public AudioTypes audioType;

    public AudioSource source;
    public string clipName;
    public AudioClip audioClip;
    public bool isLoop;
    public bool playOnAwake;

    [Range(0, 1)]
    public float volume = 0.5f;
}

Not sure if this is the cause, but 1. You have have two instances: "Instance" and "instance' defined in the script. Don't think you need both of those. 2. Also, you're not checking if the instance already exists when you call Instance = this; That could cause you to have multiple AudioManager in scene if you load the main menu twice. If the instance already exists you should destroy the script and abort the Start function.

–

Thank you! To be honest why there is two instance (instance and Instance) because when I use public, it doesn't work, but because of your suggestion, I try it to move the if conditional outside the loop and it worked.

–