The question is answered, right answer was accepted
Inventory System: NullReferenceException Error
Hey guys, i seem to be non stop getting this NullReferenceException Error for nearly every single thing i code, ive asked numerous times on this forum and no one has ever replied, First it was with basic doors, had to re-write the whole script, for that to work, then it was using a sliding bookcase every time i interacted with the bookcase i will get this error, then it was a secret switch that got the same error, then it was with a key script i was using to unlock doors, the key when attempted to pick it up would give me this error, and now that i am reworking the whole key script to actually have an inventory, not just destroy the key and unlock the door, got everything setup using this tutorial Unity 3D Inventory System Full i am now up to part 5 and have become completely stuck, have been following this to the exact step through out the whole series, and now I'm getting that same annoying error when i attempt to pick the key up, the error points to something that helps me in no way to try and rectify this issue, I've gone back through the series twice now to see if i missed something, and no, i haven't, everything is exactly how the youtuber has it, but mine just magically causes me issues, as it always has when I've tried to interact with items, i even went to a point of instead of using an actual key object, to copy the EXACT same way the youtuber did it with a cube rather than an actual object but still the same error, i honestly cant understand what I'm doing wrong, how can i mess up every piece of code i do, that gives me the exact same error every single time, for every single script, this is the error i am receiving, it in no way explains what is wrong, and my script is exactly the same as the youtubers, besides he is using 5+ items and I'm just trying to at least get 1 single item to work, before i bother putting myself through multiple errors at a time.
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
ItemTemplate.Update () (at Assets/Scripts/InventorySystem/ItemTemplate.js:21)
this error leads to this script
import System.Collections.Generic;
private var _gameManager : GameObject;
private var _player : GameObject;
private var _items : Items;
private var _inv : Inventory;
private var _canGet : boolean = false;
function Start() {
_gameManager = GameObject.FindGameObjectWithTag("GameManager");
_player = GameObject.FindGameObjectWithTag("Player");
_items = _gameManager.GetComponent(Items);
_inv = _player.GetComponent(Inventory);
}
function Update () {
if(Input.GetKeyDown(KeyCode.E) && _canGet){
_inv.mainInvetoryList.Add(_items.itemsList[0]); //HERE IS THE ERROR
Destroy(gameObject);
}
}
function OnTriggerEnter (other : Collider){
if(other.gameObject.tag == "Player"){
_canGet = true;
}
}
function OnTriggerExit (other : Collider){
if(other.gameObject.tag == "Player"){
_canGet = false;
}
}
function OnGUI(){
if(_canGet){
GUI.Box(new Rect(Screen.width / 2 - 75, Screen.height - 100, 200, 30), "Press 'E' to Pickup " + this.name);
}
}
i just need a good kick in the right direction as to why i am ALWAYS.... AAALLLLWWWAAAAYYYYSSS getting this error, so i can stop it from f**king everything I'm trying to achieve in almost every script. is it because I'm using outdated tutorials, because honestly this is all i have, its so, SOOO hard to get any help with this sort of stuff and i can't learn without being taught, i cant be the only person getting this error, and if i am, then what am i doing that makes this error come out? it seems to only ever be when I'm using Entry Colliders and Raycasts on Colliders when Interacting with them.
I know this is going to make a Huge question even bigger, but these are my current scripts for my inventory system....
Inventory.js
import System.Collections.Generic;
private var _invBackDrop : Texture2D;
private var _equipmentBackDrop : Texture2D;
private var _windowPosition : Vector2 = Vector2(0,0);
private var _windowSize : Vector2 = Vector2(Screen.width, Screen.height);
private var _windowRect : Rect = new Rect(_windowPosition.x, _windowPosition.y, _windowSize.x, _windowSize.y);
private var _player : GameObject;
private var _gameManager : GameObject;
private var _charMotor : UnityStandardAssets.Characters.FirstPerson.FirstPersonController;
private var _items : Items;
private var _equipmentManager : EquipmentManager;
var mainInventoryList : List.<ItemHandler> = new List.<ItemHandler>();
private var _display : boolean = false;
private var _displayEquipScreen : boolean = false;
private var _displayInventoryScreen : boolean = true;
function Start () {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
_invBackDrop = Resources.Load("Backgrounds/InventoryBackground", Texture2D);
_equipmentBackDrop = Resources.Load("Backgrounds/EquipmentBackground", Texture2D);
_player = GameObject.FindGameObjectWithTag("Player");
_gameManager = GameObject.FindGameObjectWithTag("GameManager");
_charMotor = _player.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController);
_items = _gameManager.GetComponent(Items);
_equipmentManager = _gameManager.GetComponent(EquipmentManager);
}
function Update () {
if(Input.GetKeyDown(KeyCode.Tab) && !_display)
{
_display = true;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
_charMotor.enabled = false;
} else if(Input.GetKeyDown(KeyCode.Tab) && _display)
{
_display = false;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
_charMotor.enabled = true;
}
}
function OnGUI() {
if(_display){
if(_displayInventoryScreen){
GUI.DrawTexture(_windowRect, _invBackDrop, ScaleMode.StretchToFill);
for(var x = 0; x < mainInventoryList.Count; x++){
if(GUI.Button(Rect(Screen.width / 2, Screen.height / 2, 120, 20), GUIContent(" " + mainInventoryList[x].name, mainInventoryList[x].icon))){
}
}
}
}
}
EquipmentManager.js
import System.Collections.Generic;
var equipmentList : List.<ItemHandler> = new List.<ItemHandler>();
private var itemIcon : Texture2D;
var update : boolean = false;
function Start() {
//Equipment [0 = RightHand, 1 = LeftHand]
//Right Hand
equipmentList.Add(new ItemHandler(0, itemIcon, "", "", false, ItemType.RightHand));
//Left Hand
equipmentList.Add(new ItemHandler(0, itemIcon, "", "", false, ItemType.LeftHand));
}
function Update () {
//Right Hand
if(!equipmentList[0].equipped && update){
equipmentList[0] = (new ItemHandler(0, itemIcon, "", "", false, ItemType.RightHand));
update = false;
}
//Left Hand
if(!equipmentList[1].equipped && update){
equipmentList[1] = (new ItemHandler(0, itemIcon, "", "", false, ItemType.LeftHand));
update = false;
}
}
Items.js
import System.Collections.Generic;
var itemsList : List.<ItemHandler> = new List.<ItemHandler>();
private var redkey : Texture2D;
function Start () {
//START ICONS
redkey = Resources.Load("Icons/redkey", Texture2D);
//END ICONS
//START ITEMS
//START KEY ITEMS
itemsList.Add(ItemHandler(1, redkey, "Red Key", "This Key looks like it fits a Red Lock", false, ItemType.RightHand));
//END KEY ITEMS
}
ItemHandler.js
#pragma strict
public class ItemHandler {
var itemID : int;
var icon : Texture2D;
var name : String;
var description : String;
var equipped : boolean = false;
var itemType : ItemType;
}
function ItemHandler(ID : int, ItemIcon : Texture2D, ItemName : String, ItemDescription : String, ItemEquipped : boolean, Type : ItemType){
this.itemID = ID;
this.icon = ItemIcon;
this.name = ItemName;
this.description = ItemDescription;
this.equipped = ItemEquipped;
this.itemType = Type;
}
enum ItemType {
RightHand,
LeftHand
}
any kick in the right direction, or even an explanation as to why i always get this error is much appreciated and may just set me on the right track and allow me to further fix future issues like this. I'm just so SICK of seeing this error, because it helps me in no way as to what it is that's actually wrong, I've searched and looked at almost every article about this error and it doesn't make sense as its nothing related to why I'm getting my error... Thank you very much for taking your time to read this, hopefully i can get this sorted to get my Development back on track.
Answer by tanoshimi · Dec 28, 2016 at 10:41 AM
You've written:
_inv.mainInvetoryList.Add(_items.itemsList[0]);
instead of:
_inv.mainInventoryList.Add(_items.itemsList[0]);