Search Unity

Question Object reference not set to an instance of an object

Discussion in 'Scripting' started by aiyan21, Mar 14, 2023.

  1. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    Hello, I keep getting the error:
    upload_2023-3-14_15-49-31.png
    even though I already assigned it to the mentioned slider: (
    upload_2023-3-14_15-59-15.png
    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:

    Code (CSharp):
    1. public class Soundmanager : MonoBehaviour
    2. {
    3.     public static Soundmanager Instance;
    4.     public Sound[] musicSounds, sfxSounds;
    5.  
    6.  
    7.     [SerializeField]
    8.     public AudioSource mSource, sfxSource;
    9.     [SerializeField]
    10.     public Slider musicSlider;
    11.     [SerializeField]
    12.     public Slider sfxSlider;
    13.  
    14.     public void Awake()
    15.     {
    16.  
    17.         if (Instance == null)
    18.         {
    19.             Instance = this;
    20.             DontDestroyOnLoad(gameObject);
    21.         }
    22.         else
    23.         {
    24.             Destroy(gameObject);
    25.             return;
    26.         }
    27.  
    28.     public void Start()
    29.     {
    30.         LoadValues();
    31.     }
    32.  
    33.     public void musicVolume(float volume)
    34.     {
    35.      (Error line)   mSource.volume = musicSlider.value;
    36.         PlayerPrefs.SetFloat("MUSICVOLUME", mSource.volume);
    37.  
    38.         sfxSource.volume = sfxSlider.value;
    39.         PlayerPrefs.SetFloat("SFXVOLUME", sfxSource.volume);
    40.     }
    41.  
    42.     void LoadValues()
    43.     {
    44.         mSource.volume = 1f;
    45.         sfxSource.volume = 1f;
    46.         mSource.volume = PlayerPrefs.GetFloat("MUSICVOLUME");
    47.         musicSlider.value = mSource.volume;
    48.         //AudioListener.volume = musicSource.volume;
    49.  
    50.         sfxSource.volume = PlayerPrefs.GetFloat("SFXVOLUME");
    51.         sfxSlider.value = sfxSource.volume;
    52.         //AudioListener.volume = sfxSource.volume;
    53.     }
    I would appreciate it very much if you can help me!!
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    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?
     
  3. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    yes, i did already assign the audio source that's why i was also very confused...

    here's my full script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7. using System;
    8.  
    9. public class Soundmanager : MonoBehaviour
    10. {
    11.     public static Soundmanager Instance;
    12.     public Sound[] musicSounds, sfxSounds;
    13.  
    14.  
    15.     [SerializeField]
    16.     public AudioSource mSource, sfxSource;
    17.     [SerializeField]
    18.     public Slider musicSlider;
    19.     [SerializeField]
    20.     public Slider sfxSlider;
    21.  
    22.     public void Awake()
    23.     {
    24.  
    25.         if (Instance == null)
    26.         {
    27.             Instance = this;
    28.             DontDestroyOnLoad(gameObject);
    29.         }
    30.         else
    31.         {
    32.             Destroy(gameObject);
    33.             return;
    34.         }
    35.  
    36.         if (SceneManager.GetActiveScene().buildIndex == 0)
    37.         {
    38.             PlayMusic("Main");
    39.             Debug.Log("playing main");
    40.         }
    41.         else if (SceneManager.GetActiveScene().buildIndex == 1)
    42.         {
    43.             StopMusic("Main");
    44.             PlayMusic("Intro");
    45.             Debug.Log("playing intro");
    46.         }
    47.         else if (SceneManager.GetActiveScene().buildIndex == 2)
    48.         {
    49.             StopMusic("Intro");
    50.             PlayMusic("Quiz");
    51.             Debug.Log("playing quiz");
    52.         }
    53.         else if (SceneManager.GetActiveScene().buildIndex == 6)
    54.         {
    55.             StopMusic("Quiz");
    56.             PlayMusic("Results");
    57.             Debug.Log("playing results");
    58.         }
    59.     }
    60.     public void Start()
    61.     {
    62.         LoadValues();
    63.     }
    64.  
    65.     public void musicVolume(float volume)
    66.     {
    67.         mSource.volume = musicSlider.value;
    68.         PlayerPrefs.SetFloat("MUSICVOLUME", mSource.volume);
    69.  
    70.         sfxSource.volume = sfxSlider.value;
    71.         PlayerPrefs.SetFloat("SFXVOLUME", sfxSource.volume);
    72.     }
    73.  
    74.     void LoadValues()
    75.     {
    76.         mSource.volume = 1f;
    77.         sfxSource.volume = 1f;
    78.         mSource.volume = PlayerPrefs.GetFloat("MUSICVOLUME");
    79.         musicSlider.value = mSource.volume;
    80.         //AudioListener.volume = musicSource.volume;
    81.  
    82.         sfxSource.volume = PlayerPrefs.GetFloat("SFXVOLUME");
    83.         sfxSlider.value = sfxSource.volume;
    84.         //AudioListener.volume = sfxSource.volume;
    85.     }
    86.  
    87.     public void PlayMusic(string name)
    88.     {
    89.         Sound s = Array.Find(musicSounds, x => x.name == name);
    90.  
    91.         if (s == null)
    92.         {
    93.             Debug.Log("Sound not found :(");
    94.             return;
    95.         }
    96.         else
    97.         {
    98.             mSource.clip = s.clip;
    99.             mSource.Play();
    100.         }
    101.     }
    102.  
    103.     public void StopMusic(string name)
    104.     {
    105.         Sound s = Array.Find(musicSounds, x => x.name == name);
    106.  
    107.         if (s == null)
    108.         {
    109.             Debug.Log("Sound not found :(");
    110.         }
    111.         else
    112.         {
    113.             mSource.clip = s.clip;
    114.             mSource.Stop();
    115.         }
    116.     }
    117.  
    118.     public void PlaySFX(string name)
    119.     {
    120.         Sound s = Array.Find(sfxSounds, x => x.name == name);
    121.  
    122.         if (s == null)
    123.         {
    124.             Debug.Log("Sound not found :(");
    125.         }
    126.         else
    127.         {
    128.             sfxSource.PlayOneShot(s.clip);
    129.         }
    130.     }
    the audio source:
    upload_2023-3-14_16-14-23.png
     
  4. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    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 :"""(

    Code (CSharp):
    1.  Debug.Log(musicSlider);
    2.         mSource.volume = musicSlider.value;
    3.         PlayerPrefs.SetFloat("MUSICVOLUME", mSource.volume);
    upload_2023-3-14_16-43-27.png
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    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.

    Code (CSharp):
    1. Debug.Log($"Music Slider: {musicSlider}", this);
     
  6. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    it gave me this, although I don't really understand
    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?
     

    Attached Files:

  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    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.
     
  8. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    can you tell me how? I only discovered prefabs like yesterday, very very clueless as to how to use it properly :''' (
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    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.
     
    aiyan21 likes this.
  10. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7. using System;
    8.  
    9. public class Soundmanager : MonoBehaviour
    10. {
    11.     public static Soundmanager Instance;
    12.     public Sound[] musicSounds, sfxSounds;
    13.  
    14.  
    15.     [SerializeField]
    16.     public AudioSource mSource, sfxSource;
    17.     [SerializeField]
    18.     public Slider musicSlider;
    19.     [SerializeField]
    20.     public Slider sfxSlider;
    21.  
    22.  
    23. public void Awake()
    24.     {
    25.  
    26.         if (Instance == null)
    27.         {
    28.             Instance = this;
    29.             DontDestroyOnLoad(gameObject);
    30.         }
    31.         else
    32.         {
    33.             Destroy(gameObject);
    34.             return;
    35.         }
    36.  
    37.         if (SceneManager.GetActiveScene().buildIndex == 0)
    38.         {
    39.             PlayMusic("Main");
    40.             Debug.Log("playing main");
    41.         }
    42.         else if (SceneManager.GetActiveScene().buildIndex == 1)
    43.         {
    44.             StopMusic("Main");
    45.             PlayMusic("Intro");
    46.             Debug.Log("playing intro");
    47.         }
    48.         else if (SceneManager.GetActiveScene().buildIndex == 2)
    49.         {
    50.             StopMusic("Intro");
    51.             PlayMusic("Quiz");
    52.             Debug.Log("playing quiz");
    53.         }
    54.         else if (SceneManager.GetActiveScene().buildIndex == 6)
    55.         {
    56.             StopMusic("Quiz");
    57.             PlayMusic("Results");
    58.             Debug.Log("playing results");
    59.         }
    60.     }
    edit: nvm!! i kinda fixed it by putting the play music code on my scene switcher instead. thank you anyway!! : D
     
    spiney199 likes this.