Search Unity

Question Some error that doesn't make sense

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

  1. squarekiki

    squarekiki

    Joined:
    Dec 28, 2020
    Posts:
    39
    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. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    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

    Kurt-Dekker

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

    https://forum.unity.com/threads/obj...n-instance-of-an-object.1089445/#post-7016797

    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

    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:
    11,847
    In his defense, from the timestamps it looks like he was making duplicates before anyone responded :p