Search Unity

ERROR: NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by HarmlessRaccon, Feb 17, 2020.

  1. HarmlessRaccon

    HarmlessRaccon

    Joined:
    Mar 22, 2018
    Posts:
    7
    Hello everyone!

    I am making a game in which you control a ball that moves up, down, left and right and shoots bullets at incoming enemies. The problem is: I'm trying to not only destroy the enemy object, but also regenerate 1 bullet upon collision with the enemy, but even after trying many ways of detecting the object it always gives me the nullreferenceexception error. The debug successfully informs me that my bullet collided with "Enemy" or "Enemy (Clone)" depending on the occasion, yet after trying gameObject.name, gameObject.tag and compareTag it still does not work. here is my code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class bulletScript : MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.     public float moveSpeed = 9;
    9.     Vector2 movement;
    10.     public GameObject bullet;
    11.     playerMovement regen;
    12.     public GameObject Enemy;
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         movement.x = 1;
    17.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    18.         StartCoroutine(destroy());
    19.     }
    20.  
    21.     void OnCollisionEnter2D(Collision2D collision)
    22.     {
    23.         Debug.Log("Collision with: " + collision.gameObject.name);
    24.         regen = GetComponent<playerMovement>();
    25.         float regenAmmo = regen.bulletAmmo;
    26.         GameObject enemy = GameObject.Find("Enemy");
    27.         GameObject enemy1 = GameObject.Find("Enemy (Clone)");
    28.         if (collision.gameObject == enemy || collision.gameObject == enemy1)
    29.         {
    30.             regenAmmo++;
    31.             Destroy(Enemy);
    32.         }
    33.     }
    34.     IEnumerator destroy()
    35.     {
    36.         yield return new WaitForSeconds(3);
    37.         Destroy(bullet);
    38.     }
    39. }
    40.  
    Here is the "bulletAmmo" float i'm accesing

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerMovement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.     public Rigidbody2D rb;
    9.     Vector2 movement;
    10.     public GameObject Bullet;
    11.     public GameObject Player;
    12.     bool shootNow;
    13.     Vector3 position;
    14.     public float bulletAmmo = 3;
    15.  
    16.     void Update()
    17.     {
    18.         if (bulletAmmo > 3)
    19.         {
    20.             bulletAmmo--;
    21.         }
    22.         if (bulletAmmo <= 0)
    23.         {
    24.             destroy();
    25.         }
    26.         movement.x = Input.GetAxisRaw("Horizontal");
    27.         movement.y = Input.GetAxisRaw("Vertical");
    28.  
    29.         float Playerx = Player.transform.position.x;
    30.         float Playery = Player.transform.position.y;
    31.  
    32.         position = new Vector3(Playerx+1, Playery, 0);
    33.  
    34.         shootNow = Input.GetKeyDown("q");
    35.         if (shootNow == true)
    36.         {
    37.             Object.Instantiate(Bullet, position, Quaternion.identity);
    38.             bulletAmmo--;
    39.         }
    40.     }
    41.     void FixedUpdate()
    42.     {
    43.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    44.     }
    45.  
    46.     private void destroy()
    47.     {
    48.         Destroy(Player);
    49.     }
    50. }
    Thanks for reading!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What line in your code is the error pointing to?

    You solve null reference errors pretty much the same way every time.

    1) Go to the line of code the error points to
    2) Determine what reference type variables are used on that line, if only 1 skip to step 4
    3) Add debugging ahead of that line to determine which reference variable is null
    4) Figure out why the reference is null
    5) Make the reference not null before you try using it (this can mean actually assigning the reference, or it might mean checking if the reference is null and skipping this code if it isn't yet assigned)
     
  3. ciorbyn

    ciorbyn

    Joined:
    Oct 17, 2013
    Posts:
    138
    You should post the debug with the indication of the error line.

    In any case, if I'm not mistaken, the instance object generated by unity is "Enemy(Clone)" without space and not "Enemy (Clone)", in fact, if he doesn't recognize it, he could give NullReferenceException because he can't find it.
     
  4. HarmlessRaccon

    HarmlessRaccon

    Joined:
    Mar 22, 2018
    Posts:
    7
    it was line 25, but i fixed it by instead of assigning it to a variable i just used regen.bulletAmmo EDIT: This did not actually fix it, why does line 25 give this error?. It was also line 29, where ciorbyn was right and taking out the space fixed it. But now, even without errors, it still doesnt regen my ammo nor does it destroy the enemy gameobject, am i missing something?

    After debugging, it seems the "if" statement IS reached