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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Resolved 19.4.15f1 UnassignedRefernceException On A Var Already Assigned

Discussion in 'Scripting' started by skycat2216, Jan 2, 2021.

  1. skycat2216

    skycat2216

    Joined:
    Sep 25, 2020
    Posts:
    3
    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 https://imgur.com/YamHpUw

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

    Here's my codes:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class bullet : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     public bool hitbox;
    10.     public Transform hitCheck;
    11.     public float hitDistance;
    12.     public LayerMask hitMask;
    13.     public GameObject self;
    14.  
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         hitbox = Physics.CheckSphere
    20.             (hitCheck.position, hitDistance, hitMask);
    21.          
    22.         if(hitbox==true)
    23.         {
    24.             var BulletRecycle = new bulletpool();
    25.             BulletRecycle.Recycle(self);
    26.         }
    27.     }
    28. }
    29.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    No need to guess! Just add Debug.Log statements and provide the second argument like so:
    Code (CSharp):
    1. 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.
     
    adamgolden likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    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:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

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

    skycat2216

    Joined:
    Sep 25, 2020
    Posts:
    3
    Thanks to help.

    But, weird thing happened after I added a new conditions.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class bullet : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     public bool hitbox;
    9.     public Transform hitCheck;
    10.     public float hitDistance = 3.5f;
    11.     public LayerMask hitMask;
    12.     public GameObject self;
    13.  
    14.     public bulletpool BulletRecycle;
    15.     void Start()
    16.     {
    17.      
    18.     }
    19.     void Update()
    20.     {
    21.         hitbox = Physics.CheckSphere
    22.             (hitCheck.position, hitDistance, hitMask);
    23.         if (BulletRecycle != null && hitbox == true) //<------ the point I changed condition.
    24.         {
    25.             BulletRecycle.Recycle(self);
    26.         }
    27.          
    28.     }
    29. }
    30.  
    the UnsignedRefernceExpection disappeared!