So recently I have been learning unity from an FPS tutorial for unity 5. Everything has been working fine, but when I tried to use the gunfire script it gives this error.
ArgumentNullException: Value cannot be null.
Parameter name: source
UnityEngine.AudioSource.Play () (at <41c3f0db5b294ee7952c69a689436367>:0)
Gunfire.Update () (at Assets/Scripts/Gunfire.cs:9)
here is the code I have been using.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gunfire : MonoBehaviour
{ void Update () {
if(Input.GetButtonDown("Fire1")) {
AudioSource gunsound = GetComponent<AudioSource>();
gunsound.Play();
GetComponent<Animation>().Play("Gunshot");
}
}
// Start is called before the first frame update
void Start()
{
}
}
Is this error happening when you click the fire button? If so, have you set the audio clip on the AudioSource (either in code or through the inspector)? When you call the Play() method on the AudioSource it will play the clip that is set up in the Audio Clip of the AudioSource. If you haven’t set up the clip, then that would probably be why it is complaining that it is null.
The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
This actually does not apply here at all, this is just a byproduct of Unity’s special way of doing things.
You cannot do AudioSource source = new AudioSource();, that makes too much sense for Unity to comprehend. The Unity way of doing things is
AudioSource source;
void Start()
{
source = new AudioSource();
}
This applies to the OP’s case as well, you must do AudioSource source outside of the Update function and then in the start function do source = GetComponent<AudioSource>()
Hope that helps anyone who finds this via Google.
EDIT: Actually in my case, it seems that I had to implicitly attach an AudioSource to the object, and I couldn’t just create it in the script at all.
You can’t create any component this way. This is a hard rule of Unity. You have to use AddComponent so that Unity knows to do the C++ managed stuff as well, or put it on via the editor and reference it in a Serialised field (or GetComponent).
it tells the same as me and I dont know what happen with my code, can someone tell me?
ArgumentNullException: Value cannot be null.
Parameter name: collider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collidable : MonoBehaviour
{
public ContactFilter2D filter;
private BoxCollider2D boxCollider;
private Collider2D[ ] hits = new Collider2D[10];
protected virtual void Update()
{
//Collision work
boxCollider.OverlapCollider(filter, hits);
for (int i = 0; i < hits.Length; i++ )
{
if (hits == null) continue; OnCollide(hits*);* //The array is not cleaned up, so we do it ourself hits = null; } } protected virtual void OnCollide(Collider2D coll) { Debug.Log(coll.name); } }
Please don’t hijack threads for your own issues, create your own. This is a generic error, we don’t need a single thread detailing every time anyone gets it TBH. If you make another post, tag me in and I’ll happily go over it with you!
So the error tells you exactly what the problem is and where so read what it says. Unless you don’t know what NULL or an argument means but if that’s the case then it’s worth asking that instead.
Also, when you post code, please always use code-tags and not plain text.