ArgumentNullException: Value cannot be null

How to repair the follow nonsense?

ArgumentNullException: Value cannot be null.
Parameter name: source
UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip, System.Single volumeScale) (at <e47743e68760443e90610e227d064273>:0)
UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip) (at <e47743e68760443e90610e227d064273>:0)
Idiotic_Ruby_Controller.PlaySound (UnityEngine.AudioClip clip) (at Assets/Scripts/Idiotic_Ruby_Controller.cs:139)
HealthCollectible.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/HealthCollectible.cs:19)

The above mention scripts (Idiotic_Ruby_Controller and HealthCollectible) is under spoilers…

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

public class Idiotic_Ruby_Controller : MonoBehaviour
{
    public int maxHealth = 5;
   
    public float time_invincible = 2.0f;

    public float maxSpeed = 3.0f;
   
    public int health { get { return currentHealth; } }
   
    public GameObject projectile_prefab;
   
    public ParticleSystem Get_Hit;
   
    public ParticleSystem Consume_Health_Potion;

    int currentHealth;
   
    bool is_invincible;
    float invincible_timer;

    Rigidbody2D rigidbody2d;
    float horizontal;
    float vertical;
   
    Animator animator;
    Vector2 look_direction = new Vector2(1, 0);
   
    AudioSource audio_source;

    // Start is called before the first frame update.
    void Start()
    {
        animator = GetComponent<Animator>();
        rigidbody2d = GetComponent<Rigidbody2D>();
        currentHealth = maxHealth;
        audio_source = GetComponent<AudioSource>();
    }

    // Update is called once per frame.
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
        Vector2 move = new Vector2(horizontal, vertical);
       
        if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
        {
            look_direction.Set(move.x, move.y);
            look_direction.Normalize();
        }
       
        animator.SetFloat("Look X", look_direction.x);
        animator.SetFloat("Look Y", look_direction.y);
        animator.SetFloat("Speed", move.magnitude);
       
        if (is_invincible)
        {
            invincible_timer -= Time.deltaTime;
            if (invincible_timer < 0)
                is_invincible = false;
        }
       
        if (Input.GetKeyDown(KeyCode.X))
        {
            RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up*0.2f, look_direction, 1.5f, LayerMask.GetMask("NPC"));
           
            // All you are doing here is checking if we have a hit, then trying to find a NonPlayerCharacter script on the object the Raycast hit, and if that script exists on that object, you will display the dialog.
            if (hit.collider != null)
            {
                Non_Player_Character character = hit.collider.GetComponent<Non_Player_Character>();
                if (character != null)
                {
                    character.Display_Dialog();
                }
                //Displaying what hit character from NPC layer in the Console.
                //Debug.Log("Ray has hit the object " + hit.collider.gameObject);
            }
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            Launch();
        }
    }

    void FixedUpdate()
    {
        Vector2 position = rigidbody2d.position;
        position.x = position.x + maxSpeed*horizontal*Time.deltaTime;
        position.y = position.y + maxSpeed*vertical*Time.deltaTime;
        rigidbody2d.MovePosition(position);
    }
   
    public void ChangeHealth (int amount)
    {
        // Animation increasing health.
        if (amount > 0)
        {
            Instantiate(Consume_Health_Potion, rigidbody2d.position, Quaternion.identity);
        }

        if (amount < 0)
        {
            animator.SetTrigger("Hit");
            Instantiate(Get_Hit, rigidbody2d.position + Vector2.up*1.0f, Quaternion.identity);
           
            if (is_invincible)
                return;

            is_invincible = true;
            invincible_timer = time_invincible;
        }
            currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);

            //Making changes into UI-Healthbar.
            UI_Health_Bar.instance.SetValue(currentHealth / (float)maxHealth);
           
            //Showing health due to debug console.
            //Debug.Log(currentHealth + "/" + maxHealth);
    }
   
    //Launching cog program function.
    void Launch()
    {
        GameObject projectile_object = Instantiate(projectile_prefab, rigidbody2d.position + Vector2.up*0.5f, Quaternion.identity);
        Projectile projectile = projectile_object.GetComponent<Projectile>();
        projectile.Launch(look_direction, 300);
       
        animator.SetTrigger("Launch");
    }
   
    //Playing sounds of interacted objects.
    public void PlaySound (AudioClip clip)
    {
        audio_source.PlayOneShot(clip);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthCollectible : MonoBehaviour
{
    public AudioClip collected_clip;
   
    void OnTriggerEnter2D (Collider2D other)
    {
         Idiotic_Ruby_Controller controller = other.GetComponent<Idiotic_Ruby_Controller>();
        
         if (controller != null)
         {
            //Calling programs from main character script.
            if (controller.health < controller.maxHealth)
            {
                controller.ChangeHealth(1);
                controller.PlaySound(collected_clip);
                Destroy(gameObject);
            }
         }

        //Debug.Log("Object that entered the trigger: " + other);
    }
}

Thank you.

I had the same problem.
I fixed it removing the Ruby’s sub-element “Audio Source” in the hierarchy window, and added the “Audio Source” via (Add Component) trough the inspector, so it will be a component of Ruby and not an element on Scene.

Hope it helps.

1 Like

collected_clip is probably null since it is the argument given causing the ArgumentNullException

Yep, thank you, but me fixed it too. The main point is the following program: GetComponent(); is workin only with component programs, but not with sub-elements. Me stupid was, and no understand why the mistake appears.

Yes and no in the same time. Audio-clip connected to consumable is no null, but due to the above mentioned program working only with components, it is!

If you want to use GetComponent for searching the childrens as well, use GetComponentInChildren: Unity - Scripting API: Component.GetComponentInChildren :).

I have tried, just for interest — actually not worked.