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 Object reference not set to an instance of an object

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

  1. squarekiki

    squarekiki

    Joined:
    Dec 28, 2020
    Posts:
    30
    I have two scrips
    Code (CSharp):
    1. public class ItemManager : MonoBehaviour
    2. {
    3.    public Weapon[] WeaponList = new Weapon[2];
    4.    public Weapon weapon;
    5.  
    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.    }
    16.  
    17. }
    and
    Code (CSharp):
    1. public class WeaponPickUp : MonoBehaviour
    2. {
    3.     public Weapon item;
    4.  
    5.     private ItemManager itemmanager;
    6.  
    7.     private static int i = 0;
    8.  
    9.     public int poop;
    10.  
    11.     void Start()
    12.     {
    13.         itemmanager = GetComponent<ItemManager>();
    14.     }
    15.  
    16.     void OnTriggerEnter2D (Collider2D hitInfo)
    17.     {
    18.         if (hitInfo.gameObject.tag == "Player")
    19.         {
    20.             item = itemmanager.WeaponList[i];
    21.             i++;
    22.             PickUp();
    23.         }
    24.     }
    25.  
    26.     void PickUp()
    27.     {
    28.         Debug.Log("Picking up " + item.name);
    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. squarekiki

    squarekiki

    Joined:
    Dec 28, 2020
    Posts:
    30
    guys please
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    13,753
    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

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

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    10,809
    My guess is GetComponent<ItemManager>() returns null in WeaponPickUp.Start. Since GetComponent is designed to return null when it can't find what you ask it to, that should always be one of the first things you check when you get a null reference error involving a reference you set via GetComponent.

    Change that Start to this:
    Code (csharp):
    1. void Start()
    2. {
    3.     itemmanager = GetComponent<ItemManager>();
    4.     if (itemmanager == null)
    5.     {
    6.         Debug.Log("OOPS!  ItemManager not found");
    7.     }
    8. }
    But you must get good at solving this kind of error on your own. You'll run into it all the time, and you can't post the basically the same question 5 times a day :p Good luck, and I hope you're having fun with this :)
     
    Kurt-Dekker likes this.
unityunity