Object reference not set to an instance of an object

Hello, I keep getting the error:
8874696--1211907--upload_2023-3-14_15-49-31.png
even though I already assigned it to the mentioned slider: (
8874696--1211913--upload_2023-3-14_15-59-15.png
8874696--1211910--upload_2023-3-14_15-58-57.png
it was working earlier but it suddenly got an error, I’m really confused as to what it wants from me…

here’s my audio manager:

public class Soundmanager : MonoBehaviour
{
    public static Soundmanager Instance;
    public Sound[] musicSounds, sfxSounds;
 

    [SerializeField]
    public AudioSource mSource, sfxSource;
    [SerializeField]
    public Slider musicSlider;
    [SerializeField]
    public Slider sfxSlider;

    public void Awake()
    {

        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
            return;
        }

    public void Start()
    {
        LoadValues();
    }

    public void musicVolume(float volume)
    {
     (Error line)   mSource.volume = musicSlider.value;
        PlayerPrefs.SetFloat("MUSICVOLUME", mSource.volume);

        sfxSource.volume = sfxSlider.value;
        PlayerPrefs.SetFloat("SFXVOLUME", sfxSource.volume);
    }

    void LoadValues()
    {
        mSource.volume = 1f;
        sfxSource.volume = 1f;
        mSource.volume = PlayerPrefs.GetFloat("MUSICVOLUME");
        musicSlider.value = mSource.volume;
        //AudioListener.volume = musicSource.volume;

        sfxSource.volume = PlayerPrefs.GetFloat("SFXVOLUME");
        sfxSlider.value = sfxSource.volume;
        //AudioListener.volume = sfxSource.volume;
    }

I would appreciate it very much if you can help me!!

I’m even more confused because the error says it is on line 67, but your script doesn’t have that line (and looks like tyou have not included the whole script because there are no “using” lines in it).

Please post your full script.

But based on your indication of “error line” have you assigned all of the items you have as SerializeField (like mSource)? You say that you have assigned the slider, but have you assigned the AudioSource?

yes, i did already assign the audio source that’s why i was also very confused…

here’s my full script:

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

public class Soundmanager : MonoBehaviour
{
    public static Soundmanager Instance;
    public Sound[] musicSounds, sfxSounds;
  

    [SerializeField]
    public AudioSource mSource, sfxSource;
    [SerializeField]
    public Slider musicSlider;
    [SerializeField]
    public Slider sfxSlider;

    public void Awake()
    {

        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
            return;
        }

        if (SceneManager.GetActiveScene().buildIndex == 0)
        {
            PlayMusic("Main");
            Debug.Log("playing main");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 1)
        {
            StopMusic("Main");
            PlayMusic("Intro");
            Debug.Log("playing intro");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 2)
        {
            StopMusic("Intro");
            PlayMusic("Quiz");
            Debug.Log("playing quiz");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 6)
        {
            StopMusic("Quiz");
            PlayMusic("Results");
            Debug.Log("playing results");
        }
    }
    public void Start()
    {
        LoadValues();
    }

    public void musicVolume(float volume)
    {
        mSource.volume = musicSlider.value;
        PlayerPrefs.SetFloat("MUSICVOLUME", mSource.volume);

        sfxSource.volume = sfxSlider.value;
        PlayerPrefs.SetFloat("SFXVOLUME", sfxSource.volume);
    }

    void LoadValues()
    {
        mSource.volume = 1f;
        sfxSource.volume = 1f;
        mSource.volume = PlayerPrefs.GetFloat("MUSICVOLUME");
        musicSlider.value = mSource.volume;
        //AudioListener.volume = musicSource.volume;

        sfxSource.volume = PlayerPrefs.GetFloat("SFXVOLUME");
        sfxSlider.value = sfxSource.volume;
        //AudioListener.volume = sfxSource.volume;
    }

    public void PlayMusic(string name)
    {
        Sound s = Array.Find(musicSounds, x => x.name == name);

        if (s == null)
        {
            Debug.Log("Sound not found :(");
            return;
        }
        else
        {
            mSource.clip = s.clip;
            mSource.Play();
        }
    }

    public void StopMusic(string name)
    {
        Sound s = Array.Find(musicSounds, x => x.name == name);

        if (s == null)
        {
            Debug.Log("Sound not found :(");
        }
        else
        {
            mSource.clip = s.clip;
            mSource.Stop();
        }
    }

    public void PlaySFX(string name)
    {
        Sound s = Array.Find(sfxSounds, x => x.name == name);

        if (s == null)
        {
            Debug.Log("Sound not found :(");
        }
        else
        {
            sfxSource.PlayOneShot(s.clip);
        }
    }

the audio source:
8874720--1211922--upload_2023-3-14_16-14-23.png

i tried doing the debug.log thing to determine which was giving me the error and it was the musicSlider, idk how to fix it tho :“”"(

 Debug.Log(musicSlider);
        mSource.volume = musicSlider.value;
        PlayerPrefs.SetFloat("MUSICVOLUME", mSource.volume);

8874774--1211943--upload_2023-3-14_16-43-27.png

You probably have an extra component somewhere in the scene. Use the second overload of Debug.Log to highlight the component when you select the log entry.

Debug.Log($"Music Slider: {musicSlider}", this);

it gave me this, although I don’t really understand
8874879--1211982--upload_2023-3-14_17-45-16.png

when i double clicked it, it brought me to my sound manager prefab. what do i do with it?

8874879--1211979--upload_2023-3-14_17-43-54.png

Sounds like some of your code is referencing/calling methods on the prefab itself, and not the instance in a scene. The prefab in your project files is not the same as an instance in a scene. You will want to make sure your code is acting on the instance, and not the asset.

can you tell me how? I only discovered prefabs like yesterday, very very clueless as to how to use it properly :‘’’ (

Prefabs are basically pre-made objects you can cut and paste throughout your scene. However, each copy you make is separate at runtime and have no connection to one another.

With prefabs you generally want your code to act on the instances in the scene. So if you want to reference it via the inspector, for example, drag in the instance from the scene, not the asset in your project folders. That’s the best I can describe ‘how’ to do it.

1 Like

thank you so much!!! it’s working now!! : D
but the other music (or other background music) are not playing. I have different music for some scenes and idk why it’s not working now…

here’s my code for it:

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

public class Soundmanager : MonoBehaviour
{
    public static Soundmanager Instance;
    public Sound[] musicSounds, sfxSounds;
  

    [SerializeField]
    public AudioSource mSource, sfxSource;
    [SerializeField]
    public Slider musicSlider;
    [SerializeField]
    public Slider sfxSlider;


public void Awake()
    {

        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
            return;
        }

        if (SceneManager.GetActiveScene().buildIndex == 0)
        {
            PlayMusic("Main");
            Debug.Log("playing main");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 1)
        {
            StopMusic("Main");
            PlayMusic("Intro");
            Debug.Log("playing intro");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 2)
        {
            StopMusic("Intro");
            PlayMusic("Quiz");
            Debug.Log("playing quiz");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 6)
        {
            StopMusic("Quiz");
            PlayMusic("Results");
            Debug.Log("playing results");
        }
    }

edit: nvm!! i kinda fixed it by putting the play music code on my scene switcher instead. thank you anyway!! : D

1 Like