Bounce Audio Problem

I was following the course but then suddenly I started receiving the error :
ArgumentNullException: Value cannot be null.
Parameter name: source
UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip, System.Single volumeScale) (at <8e2857b79be4468ca1c28dda75978191>:0)
Boomerang.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/Boomerang.cs:61)

The bounce audio doesn’t play and show this error everytime can anybody help ?
I think I deleted my canvas by mistake, but and when I realised I added another one again. After that I started receiving this error. Everything else works fine. In the hierarchy window, One shot audio pops and goes just like before but the audio doesn’t play.

Here’s my Code in Boomerang.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boomerang : MonoBehaviour
{
//Config Parameters
[SerializeField] Paddle paddle1;
[SerializeField] float xPush = 2f;
[SerializeField] float yPush = 15f;
[SerializeField] AudioClip boomerangSounds;
[Range(0f, 1f)] [SerializeField] float audioClipVolume = 0.3f;

//State
Vector2 paddleToBoomerangVector;
bool hasStarted = false;

//Cached Component References
AudioSource myAudioSource;

// Start is called before the first frame update
void Start()
{
    paddleToBoomerangVector = transform.position - paddle1.transform.position;
    myAudioSource = GetComponent<AudioSource>();
}

// Update is called once per frame
void Update()
{
    if(!hasStarted)
    {
        LockBoomerangToPaddle();
        LaunchOnMouseClick();
    } 
}

private void LaunchOnMouseClick()
{
    if(Input.GetMouseButtonDown(0))
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);
        hasStarted = true;
    }
}

private void LockBoomerangToPaddle()
{
    Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    transform.position = paddlePos + paddleToBoomerangVector;
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (hasStarted)
    {
        if (boomerangSounds.Length > 0)
        {
            AudioClip clip = boomerangSounds[UnityEngine.Random.Range(0, boomerangSounds.Length)];
            myAudioSource.PlayOneShot(clip, audioClipVolume);
        }
    }
}

}

Hi Preet,

Check the value of clip. Maybe it is null. If it is null, check the boomerangSounds array in your Inspector. It must not be empty.

Did this help? :slight_smile:


See also:

I wrote an if statement to check whether the clip is null or not with Debug.Log and the statement doesn’t get printed in the console so that means Clip is not null and there is an audio clip in my boomerangSounds array I removed it and added it again and again. But I still get the error.

Double click on the error message. Then add a Debug.Log to check the values of the variables in that line of code. If one of them is null, go backwards in the logic flow of your program to figure out where an object is supposed to get assigned and when the/a variable doesn’t contain a reference or loses it. Use Debug.Logs.

Okay, so I did as you suggested, I added a debug to check all the values in the PlayOneShot line of code and it turns out Audio Source was the problem. The reference made to get the Audio Component was returning null. So I checked the Inspector in the Boomerang object and It turns out I had no Audio Component in the Boomerang object. It somehow got deleted I don’t know when so that’s why it stopped playing the sound. I added the Audio Source Component again in the Inspector of the prefab and everything’s working now as before. That was a nice lesson for me to debug and find the root of the problem and be careful in the future.
Thank You Nina for your help :smile:

Good job on debugging your project yourself. I knew you would find the problem. :slight_smile:

Thank You :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.