ArgumentNullException: Value cannot be null. parameter name: Source
Hi :) i am making a 2D game were the player can jump on enemies and kind of bounce of them if that makes sense. Whenever i try to jump of a enemy i don't get the "bounce" and i get a error
ArgumentNullException: Value cannot be null. parameter name: Source
Enemy.JumpedOn () (at Assets/Scripts/Enemy.cs:23) PlayerController.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/Scripts/PlayerController.cs:100)
This s from playercontroller.cs:100
private void OnCollisionEnter2D(Collision2D other)
{ // Fiende och kollison med fiende
if (other.gameObject.tag == "Enemy")
{
Enemy enemy = other.gameObject.GetComponent<Enemy>();
if (state == State.falling)
{
enemy.JumpedOn();
Jump();
}
protected virtual void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
death = GetComponent<AudioSource>();
}
This is from Enemy.cs 23
public void JumpedOn()
{
anim.SetTrigger("Death");
death.Play();
rb.velocity = Vector2.zero;
rb.bodyType = RigidbodyType2D.Kinematic;
GetComponent<Collider2D>().enabled = false;
}
Answer by IronDragonDev · Aug 10, 2020 at 05:39 AM
I know I'm a bit late but that might help someone else, So (gameObject) where the G is a small letter means this gameobject(Player) that mean you are trying to access a function in a script that is not even exist, so try delete the gameObject it's not necessary so the code will be like this:
private void OnCollisionEnter2D(Collision2D other)
{ // Fiende och kollison med fiende
if (other.CompareTag("Enemy"))
{
Enemy enemy = other.GetComponent<Enemy>();
if (state == State.falling)
{
enemy.JumpedOn();
Jump();
}
protected virtual void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
death = GetComponent<AudioSource>();
}
hope i helped someone
other
is a Collision2D type. It does not contain the CompareTag method. you have to reference the collider, game object, rigidbody, or transform of the Collision2D variable in order to access the CompareTag method, so it is required and your code will output an error
Your answer
Follow this Question
Related Questions
Can someone make sense out of seemingly inconsistent null reference exception? 3 Answers
is my enemy selector null reference a problem? 1 Answer
GameObject only spawning 60% of the time (c#) 1 Answer
My Player Character Have Infinite Jumping + ArgumentException Error 1 Answer
Object reference not set to an instance of an object ERROR - Idk why 1 Answer