19.4.15f1 UnassignedRefernceException On A Var Already Assigned

I looked at UnassignedReferenceException: Error? - Unity Forum

But, I already assigned a transform in inspector , and I’m pretty sure there’s no other object had this script

or Imgur: The magic of the Internet

Also, the error shows only at the moment I activated the bullets

Here’s my codes:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bullet : MonoBehaviour
{
    // Start is called before the first frame update
    public bool hitbox;
    public Transform hitCheck;
    public float hitDistance;
    public LayerMask hitMask;
    public GameObject self;
  

    // Update is called once per frame
    void Update()
    {
        hitbox = Physics.CheckSphere
            (hitCheck.position, hitDistance, hitMask);
          
        if(hitbox==true)
        {
            var BulletRecycle = new bulletpool();
            BulletRecycle.Recycle(self);
        }
    }
}

No need to guess! Just add Debug.Log statements and provide the second argument like so:Debug.Log("test", this.gameObject);
When you do that you can click on the log in the console and it will take you to the object that produced the log in the hierarchy window.

1 Like

Maybe you have more than one copy of this script!

The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!

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.

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.

Thanks to help.

But, weird thing happened after I added a new conditions.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour
{
    // Start is called before the first frame update
    public bool hitbox;
    public Transform hitCheck;
    public float hitDistance = 3.5f;
    public LayerMask hitMask;
    public GameObject self;

    public bulletpool BulletRecycle;
    void Start()
    {
      
    }
    void Update()
    {
        hitbox = Physics.CheckSphere
            (hitCheck.position, hitDistance, hitMask);
        if (BulletRecycle != null && hitbox == true) //<------ the point I changed condition.
        {
            BulletRecycle.Recycle(self);
        }
          
    }
}

the UnsignedRefernceExpection disappeared!