Search Unity

Audio Unity Error : ArgumentNullException: Value cannot be null

Discussion in 'Audio & Video' started by Error-102, Aug 11, 2021.

  1. Error-102

    Error-102

    Joined:
    Jan 20, 2021
    Posts:
    9
    Hi. I'm creating a game that includes triggering audio when the player collides with a certain map, but unity gave me an error saying "ArgumentNullException: Value cannot be null". Two things. Well, one I've got no clue what this even means, and two how do I fix it? It tells me that it's on line 12. Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MapArea : MonoBehaviour
    6. {
    7.     [SerializeField] List<Tricimal> untamedTricimals;
    8.     [SerializeField] AudioSource townClip;
    9.  
    10.     public void PlayAudioClip()
    11.     {
    12.         townClip.Play();
    13.     }
    14.  
    15.     public Tricimal GetRandomWildTricimal()
    16.     {
    17.         var untamedTricimal =  untamedTricimals[Random.Range(0, untamedTricimals.Count)];
    18.         untamedTricimal.Init();
    19.         return untamedTricimal;
    20.     }
    21. }
    22.  
     
  2. waller_g

    waller_g

    Joined:
    Jan 16, 2017
    Posts:
    12
    You haven't assigned your "townClip" property in the inspector.
     
  3. ouosakaibatsouo

    ouosakaibatsouo

    Joined:
    Jul 22, 2023
    Posts:
    3
    ArgumentNullException occurs because you tried to access a method from the AudioSource
    townClip
    but
    townClip
    is null. To fix it, you have to make sure there's an AudioSource assigned to
    townClip
    . In your case, just assign it in the inspector.

    If it helps, here's an example.