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

NullRefrenceExpection.

Discussion in 'Scripting' started by Ch267, Sep 27, 2020.

  1. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NormalZombieAttack : MonoBehaviour
    6. {
    7.     public int normalZombieDamage = 10;
    8.     private void OnTriggerEnter2D(Collider2D hitinfo)
    9.     {
    10.         PlayerHealth player = hitinfo.GetComponent<PlayerHealth>(); // try to find a player health script on the object the zombie collided with
    11.         player.TakeDamage(normalZombieDamage); // make the player take the normal amount of damage from a zombie
    12.  
    13.  
    14.          
    15.     }
    16. }
    Code (CSharp):
    1. public class Bullet : MonoBehaviour
    2. {
    3.     public float speed = 20f; // this is the speed the bullet is traveling at
    4.     public Rigidbody2D rb2d; // stores the rigidbody on this game object
    5.     public int damage = 50; // stores the amount of damage the bullets will do
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.  
    10.         rb2d.velocity = transform.right * -1 * speed; // make the bullet fly forward
    11.     }
    12.  
    13.     private void OnTriggerEnter2D(Collider2D hitInfo) // store info about the enemy we hit
    14.     {
    15.         NormalZombieHeath zombie = hitInfo.GetComponent<NormalZombieHeath>(); // try to finda health script for a normal zombie on the object we hit
    16.         zombie.TakeDamage(damage); //the zombie takes damage
    17.         Destroy(this.gameObject); // The bullet clone is destroyed
    18.     }
    19. }
    On these scripts, I am receiving this error a NullRefrenceExeption: Object reference not set to the instance of an object. Could someone tell me what I should change in this case so these scripts run as expected?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  3. CoolCosmos

    CoolCosmos

    Joined:
    Nov 21, 2016
    Posts:
    247
    double click on the error and see what line it's addressing. Then investigate the reason...
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    This is THE SINGLE MOST COMMON ERROR. Learn to fix it fast. Here is how:

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    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.
     
    Vryken and Ch267 like this.
  5. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    Kurt-Decker I have found the reason,
    Code (CSharp):
    1.  PlayerHealth player = hit info.GetComponent<PlayerHealth>(); // try to find a player health script on the object the zombie collided with
    but I have yet to find why the player variable is null if I had to take a guess, hitInfo is null, but I could be very wrong, I'm rather new to this.
     
  6. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    Thank you everyone who replied to this question! I have fixed the error, in case anyone else runs into my situation, it was due to two different triggers having the same name. Just me being clumsy with names that's all.
     
    Last edited: Sep 28, 2020
    Kurt-Dekker likes this.