I cant figure out this Object reference not set to an instance error

I am working with three scripts, PlayerControl, Ammo, and Attack.

  • When a player performs an attack, isLightAttack is set to true in PlayerControl and Shoot() is executed.

  • Ammo is attached to the bullet getting sent out and attack.DealDamage(collision) is triggered.

  • DealDamage() is then called in the Attack script and Debug.Log(“Deal Damage Launched”); prints but then I get the following error.

  • Here is the error:
    NullReferenceException: Object reference not set to an instance of an object
    Attack.DealDamage (UnityEngine.Collider2D collision) (at Assets/Scripts/Weapons/Attacks/Attack.cs:50)
    Ammo.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Scripts/Weapons/Ammo/Ammo.cs:68)

I have been trying to figure out what I could be doing wrong but have not made much progress. Any help or tips are appreciated.

PlayerControl:

public void LightAttack(InputAction.CallbackContext context)
{
    if (context.performed)
    {
        isAttackPerformed = true;
        isLightAttack = true;
        isHeavyAttack = false;
        animator.SetTrigger("LightAttack");
        moveVector = Vector2.zero;
        Debug.Log("Light damage " + lightAttackDamage);
        //if (staminaBar != null)
        //{
        Debug.Log("Stamina Light Attack");
        StaminaBar.instance.UseStamina(lightAttackStamina);

        if (gameObject.tag == "PlayerRanged")
        {
            Shoot();
        }

        if (gameObject.tag == "Player")
        {
            StartCoroutine(ActivateAttack());
        }
       
    }
}

public void Shoot()
{
    Instantiate(playerAmmo, firePoint.position, firePoint.rotation);
}

Ammo:

 private void OnTriggerEnter2D(Collider2D collision)
{
     Attack attack = collision.GetComponent<Attack>();

     if (attack != null)
     {
         attack.DealDamage(collision);
     }
       
     DisableAmmo();
}

Attack:

    public void DealDamage(Collider2D collision)
    {
        Health health = collision.GetComponent<Health>();
        PlayerControl playerControl = collision.GetComponent<PlayerControl>();
        Debug.Log("Deal Damage Launched");

        //if (health != null)
        //{
       
        isColliding = true;
       
        //if (playerControl != null)
        //{
            if (playerControl.isLightAttack)
            {
                Debug.Log("light attack damage");
                health.TakeDamage(playerControl.lightAttackDamage);
            }

            if (playerControl.isHeavyAttack)
            {
                Debug.Log("heavy attack damage");
                health.TakeDamage(playerControl.heavyAttackDamage);
            }
        //}
    //}
    }

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Learn how to use a debugger/breakpoints/stepping in your code line by line and you’ll find exactly what is your null object and what is causing it.

Thanks, your explanation helped me identify my problem. I needed to drag PlayerControl into my Ammo Script.