Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Audio audio source object reference not set to instance of an object

Discussion in 'Audio & Video' started by D1rektX, May 24, 2019.

  1. D1rektX

    D1rektX

    Joined:
    Nov 23, 2017
    Posts:
    1
    I have an Audio Source that plays a couple of sounds. The first two worked fine, then I added a third the exact same way and now Im getting that Error.
    Could someone explain?
    Code (CSharp):
    1. public class Audio_Manager : MonoBehaviour
    2. {
    3.     private AudioSource audioSource;
    4.     public AudioClip gameOverSound;
    5.     public AudioClip addPoints;
    6.     public AudioClip spawnBlock;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         audioSource = GetComponent<AudioSource>();
    12.     }
    13.  
    14.     public void PlayGameOver()
    15.     {
    16.         audioSource.PlayOneShot(gameOverSound);
    17.     }
    18.     public void PlayAddPoints()
    19.     {
    20.         audioSource.PlayOneShot(addPoints);
    21.     }
    22.     public void PlaySpawnSound()
    23.     {
    24.         audioSource.PlayOneShot(spawnBlock);
    25.     }
    26.  
    27. }
    28.  
     
  2. benjpointer

    benjpointer

    Joined:
    Aug 14, 2017
    Posts:
    1
    Hey man, a little more detail would be good.

    Is there one instance of this class? if so you should set the class up as a Singleton.

    Where exactly are you calling this PlaySpawnSound? is it possible you’re calling this function prior to the AudioSource actually existing?