Unity ArgumentNullException: Value cannot be null

frostysh

Newbie
Jul 31, 2018
25
6
Following this tutorial: , paragraph number four, I have faced the next error in the line 140 of my main character script-program, and in the line 19 correspondly, in the health consumable script...

C:
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:140)
HealthCollectible.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/HealthCollectible.cs:19)
You don't have permission to view the spoiler content. Log in or register now.

Cannot understand why such error, and cannot fix it for a two days.
 

Sopra

Newbie
Jul 30, 2017
72
386
Did you check in the unity inspector that you health collectible component has an audio clip assigned to its collected_clip value ?
 

frostysh

Newbie
Jul 31, 2018
25
6
Yes, of course, I have checked corresponding settings. It is an Inspector of main character, there is no should be audio resource, obviously.



And this is consumable, which has attached to it audio-file.

 

Disturbium

Newbie
Jul 10, 2020
15
14
Is the audio source on the same gameobject as the ruby_controller and not a child or parent gameobject? If it isn't then the calling GetComponent<AudioSource> will return null. Sorry if I missed something but I can't tell if the Audio source is null or the audio clip.
 

frostysh

Newbie
Jul 31, 2018
25
6
Yes, Audio Source is related to the main character game object, "Collectable.wav" is working too, I have checked it by music player.

 

Disturbium

Newbie
Jul 10, 2020
15
14
Can you try writing this code in your Playsound method and let me know if you get any of those messages.

Code:
public void PlaySound (AudioClip clip)
{
    if(audio_source == null)
        Debug.Log("Audio source is null");
    if(clip == null)
        Debug.Log("Audio clip is null");
       
    audio_source.PlayOneShot(clip);
}
 

Tompte

Member
Dec 22, 2017
216
157
Yes, of course, I have checked corresponding settings. It is an Inspector of main character, there is no should be audio resource, obviously.
Yet, there is an error. You should check again because the error you're getting is telling you that clip is null. An important part of debugging is to challenge your own assumptions. Perhaps there are other instances of that same script in your scene where the clip isn't properly set.
 
Last edited:

frostysh

Newbie
Jul 31, 2018
25
6
Can you try writing this code in your Playsound method and let me know if you get any of those messages.
It is done.
C:
Audio source is null
UnityEngine.Debug:Log (object)
Idiotic_Ruby_Controller:PlaySound (UnityEngine.AudioClip) (at Assets/Scripts/Idiotic_Ruby_Controller.cs:141)
HealthCollectible:OnTriggerEnter2D (UnityEngine.Collider2D) (at Assets/Scripts/HealthCollectible.cs:19)
Yet, there is an error. You should check again because the error you're getting is telling you that clip is null.
I know that error exist, but it is impossible to be Collected_clip null, because of next...



The main point of this instruction is to show a newbie, as far as mineself is understood, how to make optimization: we did not make "Audio Source" program for all object, but made it only for main character (the picture of the post above of few shows it)! In CollectibleHealth we only created small variable program that has meaning of corresponding audio-clip..

C:
public AudioClip Collected_clip;

Then we using large Audio Source component of main character to play that sound when main char object collides with CollectibleHealth object.

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

But this cursed madness is not working as it must!
 

Tompte

Member
Dec 22, 2017
216
157
It's not impossible. It's not cursed madness. The error is literally telling you it's null. The question you should ask yourself is why is it null?
If something seems crazy or not right, it's almost never the computer, trust me. I've made a lot of these kinds of mistakes in my days.
 
Last edited:

MoonPillow

Newbie
Apr 5, 2020
31
9
As it was said the error log gives you clear answer. Don't get too paranoid after hours of debugging. Log says that your method "PlaySound" is called with parameter "source" (or "clip", it's the same...) which equals null. The only question is what calls this method in wrong way.
Add to the method something like this:
if (clip == null)
{
Debug.LogError("blabla");
}

Put a breakpoint on it and run the game in debug mode. Check the call stack to find the culprit.
 
Last edited:

Disturbium

Newbie
Jul 10, 2020
15
14
Ok so your audio_source = GetComponent<AudioSource>(); is failing to find your audio source. Which means the audio source is nor on the same Gameobject as Ruby_Controller script, but another gameobject that is either a parent, child or not even related.

GetComponent only grabs components from the same gameobject.

Code:
void Start()
    {
        animator = GetComponent<Animator>();
        rigidbody2d = GetComponent<Rigidbody2D>();
        currentHealth = maxHealth;
        audio_source = GetComponent<AudioSource>();
    }

1659995133636.png

I was able to look at your images you posted, some reason that they weren't showing in the forum. Your audio source is on the child gameobject of the gameobject that has the Ruby_Controller. The audiosource must be on the same gameobject as the Ruby_Controller for GetComponent to work.

If it looks like this, then you have done it right.

I can offer a 2 solutions if you don't want the AudioSource on the same gameobject.

Change the Start Method to...

Code:
void Start()
    {
        animator = GetComponent<Animator>();
        rigidbody2d = GetComponent<Rigidbody2D>();
        currentHealth = maxHealth;
        audio_source = GetComponentInChildren<AudioSource>();
    }

or

Change the Start Method to...

Code:
void Start()
    {
        animator = GetComponent<Animator>();
        rigidbody2d = GetComponent<Rigidbody2D>();
        currentHealth = maxHealth;
    }
At line 33 change [AudioSource audio_source;] to [public AudioSource audio_source;]
Then manually set the audiosource in the editor-inspector.

1659995374143.png
 
Last edited:
  • Like
Reactions: frostysh

frostysh

Newbie
Jul 31, 2018
25
6
Yet, there is an error. You should check again because the error you're getting is telling you that clip is null. An important part of debugging is to challenge your own assumptions. Perhaps there are other instances of that same script in your scene where the clip isn't properly set.
Well, in this case, it was indeed so-called human-factor, the stupid English instruction was missinterpretted by mineself. To be more preciously, this part of instruction...

So you can add an Audio Source to our Ruby GameObject, and use that Audio Source to play all sounds related to the gameplay actions that Ruby does, like grabbing a health pack, throwing a cog or being hit.
Note: if you added the Audio Source to Ruby in the Scene and not in Prefab mode, apply the override in the Inspector!


As it was said the error log gives you clear answer. Don't get too paranoid after hours of debugging. Log says that your method "PlaySound" is called with parameter "source" (or "clip", it's the same...) which equals null. The only question is what calls this method in wrong way.
Put a breakpoint on it and run the game in debug mode. Check the call stack to find the culprit.
It hard to me to try to create video-game with things that actually — black boxes, and I have no idea what it doing nor how to 'debug' it, well, internet-forums like this one is helping. The null was no an audio-clip, but the Audio Source program! It is ridiculous! :cautious:
I have been advised to try use "Love2D" along with "Unity", because Unity has a lot of levels of 'architecture' (I don't really understand what is that means, and maybe it has no sense at all in this case), and Love2D has much more direct approach, but the point creating a video-game in Love2D — is like a clear page of a notebook before you... So I don't understand. o_O
Ok so your audio_source = GetComponent<AudioSource>(); is failing to find your audio source. Which means the audio source is nor on the same Gameobject as Ruby_Controller script, but another gameobject that is either a parent, child or not even related.
It is ingenious! Due to stupid instruction, I have created a child object to Ruby object, but I must create a component instead! Thank you very Mr. Donutpunch, it is working like a charm now, right after I have changed Audio Source to be component of Ruby character.

The problem has been solved!
 

Disturbium

Newbie
Jul 10, 2020
15
14
Congrats Frostysh for finally getting it solved, and good luck on the rest of your programming journey.