Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Some error that doesn't make sense

Discussion in 'Scripting' started by squarekiki, Apr 8, 2021.

  1. squarekiki


    Dec 28, 2020
    I have two scrips
    Code (CSharp):
    1. public class ItemManager : MonoBehaviour
    2. {
    3.    public Weapon[] WeaponList = new Weapon[2];
    4.    public Weapon weapon;
    6.    void Start()
    7.    {
    8.         for(int i = 0; i < WeaponList.Length; i++)
    9.         {
    10.             int rnd = Random.Range(0, WeaponList.Length);
    11.             weapon = WeaponList[rnd];
    12.             WeaponList[rnd] = WeaponList[i];
    13.             WeaponList[i] = weapon;
    14.         }
    15.    }
    17. }
    Code (CSharp):
    1. public class WeaponPickUp : MonoBehaviour
    2. {
    3.     public Weapon item;
    5.     private ItemManager itemmanager;
    7.     private static int i = 0;
    9.     public int poop;
    11.     void Start()
    12.     {
    13.         itemmanager = GetComponent<ItemManager>();
    14.     }
    16.     void OnTriggerEnter2D (Collider2D hitInfo)
    17.     {
    18.         if (hitInfo.gameObject.tag == "Player")
    19.         {
    20.             item = itemmanager.WeaponList[i];
    21.             i++;
    22.             PickUp();
    23.         }
    24.     }
    26.     void PickUp()
    27.     {
    28.         Debug.Log("Picking up " +;
    29.         Inventory.instance.Add(item);
    30.         Destroy(gameObject);
    31.     }
    32. }
    the first script holds an array of weapons and shuffles the order of them. The second script then sets the value of Weapon item to a weapon in the array. However when it gets to line item = itemmanager.WeaponList; it gives me the error Object reference not set to an instance of an object
  2. PraetorBlue


    Dec 13, 2012
    I find it very difficult to believe that your ItemManager script is attached to the same GameObject as your WeaponPickup script. That's the only way that this code would work properly:
    Code (CSharp):
    1. itemmanager = GetComponent<ItemManager>();
    Since they're not attached to the same GameObject, this code will simply set your variable to null, resulting in the error you are seeing.
  3. Kurt-Dekker


    Mar 16, 2013
    Are you simply ignoring my posts explaining how to fix this and then making fresh duplicate posts?

    You absolutely need to understand how to fix these errors. It's like learning how to eat. If you can't fix this kind of a trivial everyday super-common error, you are not going to get very far.

    I'll re-include the blurb here just so you can fix your problem. Unfortunately, there IS NO OTHER WAY than to do these following steps. That's the ONLY way.

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. 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
    - also known as: Object reference not set to an instance of an object

    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.
    Joe-Censored likes this.
  4. Joe-Censored


    Mar 26, 2013
    In his defense, from the timestamps it looks like he was making duplicates before anyone responded :p