Search Unity

NullReferenceException at Sound.Play()

Discussion in 'Scripting' started by FaZe_Fazza, Apr 11, 2021.

  1. FaZe_Fazza

    FaZe_Fazza

    Joined:
    Apr 22, 2020
    Posts:
    2
    Hi all,

    This is my first post on Unity Forums so I'm not sure of the formatting I should be using. Anyway, I have a script for an audiomanager which was working fine before. But after playtesting this morning, it's been giving me a NullReferenceException error, specifically on the method Sound.Play(). I have pasted the code below and would appreciate it if anyone could help me out. Thanks!

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(AudioSource))]
    4. [System.Serializable]
    5. public class Sound
    6. {
    7.  
    8.     public string name;
    9.     public AudioClip clip;
    10.  
    11.     [Range(0f, 1f)]
    12.     public float volume = 0.7f;
    13.     [Range(0.5f, 1.5f)]
    14.     public float pitch = 1f;
    15.  
    16.     [Range(0f, 0.5f)]
    17.     public float randomVolume = 0.1f;
    18.     [Range(0f, 0.5f)]
    19.     public float randomPitch = 0.1f;
    20.  
    21.     public bool loop = false;
    22.  
    23.     private AudioSource source;
    24.  
    25.     public void SetSource(AudioSource _source)
    26.     {
    27.         source = _source;
    28.         source.clip = clip;
    29.         source.loop = loop;
    30.     }
    31.  
    32.     public void Play()
    33.     {
    34.         source.volume = volume * (1 + Random.Range(-randomVolume / 2f, randomVolume / 2f));
    35.         source.pitch = pitch * (1 + Random.Range(-randomPitch / 2f, randomPitch / 2f));
    36.         source.Play();
    37.     }
    38.  
    39.     public void Stop()
    40.     {
    41.         source.Stop();
    42.     }
    43.  
    44. }
    45.  
    46. public class AudioManager : MonoBehaviour
    47. {
    48.  
    49.     public static AudioManager instance;
    50.  
    51.     [SerializeField]
    52.     Sound[] sounds;
    53.  
    54.     void Awake()
    55.     {
    56.         if (instance != null)
    57.         {
    58.             if (instance != this)
    59.             {
    60.                 Destroy(this.gameObject);
    61.             }
    62.         }
    63.         else
    64.         {
    65.             instance = this;
    66.             DontDestroyOnLoad(this);
    67.         }
    68.     }
    69.  
    70.     void Start()
    71.     {
    72.         for (int i = 0; i < sounds.Length; i++)
    73.         {
    74.             GameObject _go = new GameObject("Sound_" + i + "_" + sounds[i].name);
    75.             _go.transform.SetParent(this.transform);
    76.             sounds[i].SetSource(_go.AddComponent<AudioSource>());
    77.         }
    78.  
    79.         PlaySound("Music");
    80.     }
    81.  
    82.     public void PlaySound(string _name)
    83.     {
    84.         for (int i = 0; i < sounds.Length; i++)
    85.         {
    86.             if (sounds[i].name == _name)
    87.             {
    88.                 sounds[i].Play();
    89.                 return;
    90.             }
    91.         }
    92.  
    93.         // no sound with _name
    94.         Debug.LogWarning("AudioManager: Sound not found in list, " + _name);
    95.     }
    96.  
    97.     public void StopSound(string _name)
    98.     {
    99.         for (int i = 0; i < sounds.Length; i++)
    100.         {
    101.             if (sounds[i].name == _name)
    102.             {
    103.                 sounds[i].Stop();
    104.                 return;
    105.             }
    106.         }
    107.  
    108.         // no sound with _name
    109.         Debug.LogWarning("AudioManager: Sound not found in list, " + _name);
    110.     }
    111.  
    112. }
    113.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Add Debug.Log calls and figure out what is null. Then go from there. That's really all we can tell you at this point. Something is null, now it's figuring out what is null.
     
  3. FaZe_Fazza

    FaZe_Fazza

    Joined:
    Apr 22, 2020
    Posts:
    2
    Yep it's all good. I've fixed it now.
    Thanks.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Excellent!

    Remember, the answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.