Search Unity

Help, How to solve this NullReferenceException? [DONE]

Discussion in 'Scripting' started by Kiou-23, Sep 16, 2017.

  1. Kiou-23

    Kiou-23

    Joined:
    Sep 16, 2017
    Posts:
    20
    So, I was trying to program a equipment item for an rpg, but when I try to call a method I get a NullReferenceException, can anyone help me to solve this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu (fileName = "New Equipment", menuName = "Inventory/Equipment")]
    6. public class Equipment : Item
    7. {
    8.     public int armorMod;
    9.     public int dmgMod;
    10.     public EquipmentSlot equipmentSlot;
    11.  
    12.     public override void Use ()
    13.     {
    14.         base.Use ();
    15.  
    16.         EquipmentManager.instance.Equip (this); // Error happens here.
    17.         RemoveFromInventory ();
    18.     }
    19. }
    20.  
    21. public enum EquipmentSlot {Head, Chest, Legs, Feet, rightHand, leftHand}
    22.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EquipmentManager : MonoBehaviour
    6. {
    7.  
    8.     #region Singleton
    9.  
    10.     public static EquipmentManager instance;
    11.  
    12.     void Awake ()
    13.     {
    14.         instance = this;
    15.     }
    16.  
    17.     #endregion
    18.  
    19.     Equipment[] currentEquip = new Equipment[6];
    20.     Inventory inventory;
    21.  
    22.     public void Equip (Equipment newItem)
    23.     {
    24.         int slotIndex = (int)newItem.equipmentSlot;
    25.         Equipment oldEquip;
    26.  
    27.         if (currentEquip [slotIndex] != null)
    28.         {
    29.             oldEquip = currentEquip [slotIndex];
    30.             Inventory.instance.Add (oldEquip);
    31.         }
    32.  
    33.         currentEquip [slotIndex] = newItem;
    34.         Inventory.instance.AddEquipment (newItem);
    35.     }
    36. }
    37.  

    Edit:
    So, I managed to make it work, there was a lot wrong with the code, and not only with the classes I posted, now I have a functioning equipment inventory (I'm so proud), thanks :D
     
    Last edited: Sep 16, 2017
  2. unit_nick

    unit_nick

    Joined:
    Jul 15, 2017
    Posts:
    23
    I think you're fail to initialize
    Code (csharp):
    1. public EquipmentSlot equipmentSlot;
    on line 10.
    Perhaps just give it a default
    Code (csharp):
    1. public EquipmentSlot equipmentSlot = (EquipmentSlot)0;
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Is your EquipmentManager in your scene?
     
  4. Kiou-23

    Kiou-23

    Joined:
    Sep 16, 2017
    Posts:
    20
    equipmentSlot is a enum, so I can edit it on the game object
     
  5. Kiou-23

    Kiou-23

    Joined:
    Sep 16, 2017
    Posts:
    20
    no... I'll try that and come back with the result, thx
     
  6. Kiou-23

    Kiou-23

    Joined:
    Sep 16, 2017
    Posts:
    20
    Okay, I added the EquipmentManager script under a empty game object and now I'm getting a different error lol
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    And what is that error? Post the error copied from the console, that helps. It's good that you did note what line your error is at, but also adding the console message is a big help as well.
     
  8. Kiou-23

    Kiou-23

    Joined:
    Sep 16, 2017
    Posts:
    20
    I'll do that next time I need some help, thx, but I've fixed this problem already