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);
}
}
}
}