0
\$\begingroup\$

I am working on a game and I am trying to add a sound effect.

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

public class PlayerController : MonoBehaviour
{
    private Rigidbody playerbody;
    private Animator playerAnim;
    private bool grounded;
    public bool gameover;
    public float jumpforce;
    public float gravmod;
    public ParticleSystem explosion;
    public ParticleSystem dirt;
    public AudioClip jumpSound;
    public AudioClip crashSound;
    private AudioSource playerAudio;

    // Start is called before the first frame update
    void Start()
    {
        playerbody = GetComponent<Rigidbody>();
        playerAnim = GetComponent<Animator>();
        playerAudio = GetComponent<AudioSource>();
        Physics.gravity *= gravmod;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && grounded && gameover == false)
        {
            playerAudio.PlayOneShot(jumpSound, 1.0f);
            playerbody.AddForce(Vector3.up * jumpforce, ForceMode.Impulse);
            playerAnim.SetTrigger("Jump_trig");
            grounded = false;
            dirt.Stop();
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            grounded = true;
            dirt.Play();
        }
        if (collision.gameObject.CompareTag("Obstacle"))
        {
            playerAudio.PlayOneShot(crashSound, 1.0f);
            gameover = true;
            playerAnim.SetBool("Death_b", true);
            playerAnim.SetInteger("DeathType_int", 1);
            explosion.Play();
            dirt.Stop();
        }
    }
}

At playerAudio.PlayOneShot, it just won't play and the rest of that part of the code stops working. It returns ArgumentNullException: Value cannot be null even though jumpSound and crashSound are both clearly defined. What is wrong?

\$\endgroup\$
3
  • \$\begingroup\$ This can happen sometimes if you have two instances of your script in your project/scene. You remembered to set the audioclips in the inspector on one, but missed assigning them on the other. \$\endgroup\$ – DMGregory Nov 29 '20 at 4:41
  • \$\begingroup\$ I used this script twice, both in other files but there is no place to specify what audio files I am using. Neither of these files call upon the audio file either. I do not think there any other instances of this file in my project. \$\endgroup\$ – John Liu Nov 29 '20 at 14:28
  • \$\begingroup\$ jumpSound & crashSound are defined, but it doesn't look like either of them has been initialized anywhere in the code. How are you setting their values (or put differently, how are you indicating what sound/file/resource they correspond to)? \$\endgroup\$ – Pikalek Dec 1 '20 at 4:56
2
\$\begingroup\$

Change your code like this:

if (collision.gameObject.CompareTag("Obstacle")) {
    //log error messages indicating which GameObject is missing a reference value
    if (playerAudio == null) Debug.LogError("playerAudio is null on " + gameObject.name);
    if (crashSound == null) Debug.LogError("crashSound is null on " + gameObject.name);

    playerAudio.PlayOneShot(crashSound, 1.0f);
    gameover = true;
    playerAnim.SetBool("Death_b", true);
    playerAnim.SetInteger("DeathType_int", 1);
    explosion.Play();
    dirt.Stop();
}

Do the same for the other function as well. This will log error messages that will tell you which GameObject has the problem.

Logging like this should usually be the first thing you try when code isn't working like it should.

\$\endgroup\$
1
\$\begingroup\$

I got the same error while doing the guided practice project from Unity Learn.

I have fixed it by adding an Audio Source component in the Play:

  1. Select the Play gameObject in the Hierarchy
  2. Click the Add Component button in the Inspector and select "audioSource"

Hope my answer could help other Unity learners.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.