Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved ArgumentNullException: Value cannot be null

Discussion in 'Audio & Video' started by Frostysh, Aug 7, 2022.

  1. Frostysh

    Frostysh

    Joined:
    Jul 11, 2022
    Posts:
    66
    How to repair the follow nonsense?
    Code (CSharp):
    1. ArgumentNullException: Value cannot be null.
    2. Parameter name: source
    3. UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip, System.Single volumeScale) (at <e47743e68760443e90610e227d064273>:0)
    4. UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip) (at <e47743e68760443e90610e227d064273>:0)
    5. Idiotic_Ruby_Controller.PlaySound (UnityEngine.AudioClip clip) (at Assets/Scripts/Idiotic_Ruby_Controller.cs:139)
    6. HealthCollectible.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/HealthCollectible.cs:19)
    The above mention scripts (Idiotic_Ruby_Controller and HealthCollectible) is under spoilers...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Idiotic_Ruby_Controller : MonoBehaviour
    6. {
    7.     public int maxHealth = 5;
    8.    
    9.     public float time_invincible = 2.0f;
    10.  
    11.     public float maxSpeed = 3.0f;
    12.    
    13.     public int health { get { return currentHealth; } }
    14.    
    15.     public GameObject projectile_prefab;
    16.    
    17.     public ParticleSystem Get_Hit;
    18.    
    19.     public ParticleSystem Consume_Health_Potion;
    20.  
    21.     int currentHealth;
    22.    
    23.     bool is_invincible;
    24.     float invincible_timer;
    25.  
    26.     Rigidbody2D rigidbody2d;
    27.     float horizontal;
    28.     float vertical;
    29.    
    30.     Animator animator;
    31.     Vector2 look_direction = new Vector2(1, 0);
    32.    
    33.     AudioSource audio_source;
    34.  
    35.     // Start is called before the first frame update.
    36.     void Start()
    37.     {
    38.         animator = GetComponent<Animator>();
    39.         rigidbody2d = GetComponent<Rigidbody2D>();
    40.         currentHealth = maxHealth;
    41.         audio_source = GetComponent<AudioSource>();
    42.     }
    43.  
    44.     // Update is called once per frame.
    45.     void Update()
    46.     {
    47.         horizontal = Input.GetAxis("Horizontal");
    48.         vertical = Input.GetAxis("Vertical");
    49.         Vector2 move = new Vector2(horizontal, vertical);
    50.        
    51.         if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
    52.         {
    53.             look_direction.Set(move.x, move.y);
    54.             look_direction.Normalize();
    55.         }
    56.        
    57.         animator.SetFloat("Look X", look_direction.x);
    58.         animator.SetFloat("Look Y", look_direction.y);
    59.         animator.SetFloat("Speed", move.magnitude);
    60.        
    61.         if (is_invincible)
    62.         {
    63.             invincible_timer -= Time.deltaTime;
    64.             if (invincible_timer < 0)
    65.                 is_invincible = false;
    66.         }
    67.        
    68.         if (Input.GetKeyDown(KeyCode.X))
    69.         {
    70.             RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up*0.2f, look_direction, 1.5f, LayerMask.GetMask("NPC"));
    71.            
    72.             // 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.
    73.             if (hit.collider != null)
    74.             {
    75.                 Non_Player_Character character = hit.collider.GetComponent<Non_Player_Character>();
    76.                 if (character != null)
    77.                 {
    78.                     character.Display_Dialog();
    79.                 }
    80.                 //Displaying what hit character from NPC layer in the Console.
    81.                 //Debug.Log("Ray has hit the object " + hit.collider.gameObject);
    82.             }
    83.         }
    84.  
    85.         if (Input.GetKeyDown(KeyCode.C))
    86.         {
    87.             Launch();
    88.         }
    89.     }
    90.  
    91.     void FixedUpdate()
    92.     {
    93.         Vector2 position = rigidbody2d.position;
    94.         position.x = position.x + maxSpeed*horizontal*Time.deltaTime;
    95.         position.y = position.y + maxSpeed*vertical*Time.deltaTime;
    96.         rigidbody2d.MovePosition(position);
    97.     }
    98.    
    99.     public void ChangeHealth (int amount)
    100.     {
    101.         // Animation increasing health.
    102.         if (amount > 0)
    103.         {
    104.             Instantiate(Consume_Health_Potion, rigidbody2d.position, Quaternion.identity);
    105.         }
    106.  
    107.         if (amount < 0)
    108.         {
    109.             animator.SetTrigger("Hit");
    110.             Instantiate(Get_Hit, rigidbody2d.position + Vector2.up*1.0f, Quaternion.identity);
    111.            
    112.             if (is_invincible)
    113.                 return;
    114.  
    115.             is_invincible = true;
    116.             invincible_timer = time_invincible;
    117.         }
    118.             currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    119.  
    120.             //Making changes into UI-Healthbar.
    121.             UI_Health_Bar.instance.SetValue(currentHealth / (float)maxHealth);
    122.            
    123.             //Showing health due to debug console.
    124.             //Debug.Log(currentHealth + "/" + maxHealth);
    125.     }
    126.    
    127.     //Launching cog program function.
    128.     void Launch()
    129.     {
    130.         GameObject projectile_object = Instantiate(projectile_prefab, rigidbody2d.position + Vector2.up*0.5f, Quaternion.identity);
    131.         Projectile projectile = projectile_object.GetComponent<Projectile>();
    132.         projectile.Launch(look_direction, 300);
    133.        
    134.         animator.SetTrigger("Launch");
    135.     }
    136.    
    137.     //Playing sounds of interacted objects.
    138.     public void PlaySound (AudioClip clip)
    139.     {
    140.         audio_source.PlayOneShot(clip);
    141.     }
    142. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HealthCollectible : MonoBehaviour
    6. {
    7.     public AudioClip collected_clip;
    8.    
    9.     void OnTriggerEnter2D (Collider2D other)
    10.     {
    11.          Idiotic_Ruby_Controller controller = other.GetComponent<Idiotic_Ruby_Controller>();
    12.        
    13.          if (controller != null)
    14.          {
    15.             //Calling programs from main character script.
    16.             if (controller.health < controller.maxHealth)
    17.             {
    18.                 controller.ChangeHealth(1);
    19.                 controller.PlaySound(collected_clip);
    20.                 Destroy(gameObject);
    21.             }
    22.          }
    23.  
    24.         //Debug.Log("Object that entered the trigger: " + other);
    25.     }
    26. }

    Thank you.
     
  2. EntroPics

    EntroPics

    Joined:
    Aug 20, 2022
    Posts:
    1
    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.
     
    fussd likes this.
  3. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    448
    collected_clip is probably null since it is the argument given causing the ArgumentNullException
     
  4. Frostysh

    Frostysh

    Joined:
    Jul 11, 2022
    Posts:
    66
    Yep, thank you, but me fixed it too. The main point is the following program: GetComponent<AudioSource>(); 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!
     
  5. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    448
  6. Frostysh

    Frostysh

    Joined:
    Jul 11, 2022
    Posts:
    66
    I have tried, just for interest — actually not worked.