Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Nomad-CA, Sep 28, 2023.

  1. Nomad-CA

    Nomad-CA

    Joined:
    Feb 7, 2022
    Posts:
    9
    I am working with three scripts, PlayerControl, Ammo, and Attack.

    1. When a player performs an attack, isLightAttack is set to true in PlayerControl and Shoot() is executed.
    2. Ammo is attached to the bullet getting sent out and attack.DealDamage(collision) is triggered.
    3. DealDamage() is then called in the Attack script and Debug.Log("Deal Damage Launched"); prints but then I get the following error.
    4. 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:
    Code (CSharp):
    1. public void LightAttack(InputAction.CallbackContext context)
    2. {
    3.     if (context.performed)
    4.     {
    5.         isAttackPerformed = true;
    6.         isLightAttack = true;
    7.         isHeavyAttack = false;
    8.         animator.SetTrigger("LightAttack");
    9.         moveVector = Vector2.zero;
    10.         Debug.Log("Light damage " + lightAttackDamage);
    11.         //if (staminaBar != null)
    12.         //{
    13.         Debug.Log("Stamina Light Attack");
    14.         StaminaBar.instance.UseStamina(lightAttackStamina);
    15.  
    16.         if (gameObject.tag == "PlayerRanged")
    17.         {
    18.             Shoot();
    19.         }
    20.  
    21.         if (gameObject.tag == "Player")
    22.         {
    23.             StartCoroutine(ActivateAttack());
    24.         }
    25.        
    26.     }
    27. }
    28.  
    29. public void Shoot()
    30. {
    31.     Instantiate(playerAmmo, firePoint.position, firePoint.rotation);
    32. }
    Ammo:
    Code (CSharp):
    1.  private void OnTriggerEnter2D(Collider2D collision)
    2. {
    3.      Attack attack = collision.GetComponent<Attack>();
    4.  
    5.      if (attack != null)
    6.      {
    7.          attack.DealDamage(collision);
    8.      }
    9.        
    10.      DisableAmmo();
    11. }
    Attack:
    Code (CSharp):
    1.     public void DealDamage(Collider2D collision)
    2.     {
    3.         Health health = collision.GetComponent<Health>();
    4.         PlayerControl playerControl = collision.GetComponent<PlayerControl>();
    5.         Debug.Log("Deal Damage Launched");
    6.  
    7.         //if (health != null)
    8.         //{
    9.        
    10.         isColliding = true;
    11.        
    12.         //if (playerControl != null)
    13.         //{
    14.             if (playerControl.isLightAttack)
    15.             {
    16.                 Debug.Log("light attack damage");
    17.                 health.TakeDamage(playerControl.lightAttackDamage);
    18.             }
    19.  
    20.             if (playerControl.isHeavyAttack)
    21.             {
    22.                 Debug.Log("heavy attack damage");
    23.                 health.TakeDamage(playerControl.heavyAttackDamage);
    24.             }
    25.         //}
    26.     //}
    27.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    303
    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.

     
  4. Nomad-CA

    Nomad-CA

    Joined:
    Feb 7, 2022
    Posts:
    9
    Thanks, your explanation helped me identify my problem. I needed to drag PlayerControl into my Ammo Script.