inventory null reference exceptions [solved]

i am getting this error. i have this code to initialize the inventory:

for (int i = 0; i < invcapacity; i++)
            {
                inventory[i] = ScriptableObject.CreateInstance("Item") as Item;
                inventory[i].objname = "Nothing";
                inventory[i].description = "nothing";
                inventory[i].price = 0;
                inventory[i].quantity = 0;
                inventory[i].sprite = "InvIconNothing";
                inventory[i].type = "";
                inventory[i].dr_crushing = 0;
                inventory[i].dr_cutting = 0;
                inventory[i].dr_impaling = 0;
                inventory[i].pd_crushing = 0;
                inventory[i].pd_cutting = 0;
                inventory[i].pd_impaling = 0;
                inventory[i].thrusting_modifier = 0;
                inventory[i].swinging_modifier = 0;
            }

but in order to get past the null reference exception, i had to initialize the public inventory in the editor. this worked, but now after i made some changes i am getting the null reference exception again - in the inv_add (add item to inventory) function and the inv_drop function. i made no changes to inv_add so i’m not quite sure what is going on.

if you need more context, just ask and i’ll post more of the code. i don’t want to clutter up the thread too badly.

Which line throws NullReferenceException?

Thanks.

1 Like

It looks like you are making a comparison to the Capacity property of the List and not the Count property. If invcapacity actually represents the inventory.Count property, then disregard. The Capacity could be entirely different than the Count
and that could lead to the loop checking an inventory index that has null object.

1 Like

If it’s type List, you should use either:

public List<Item> inventory;
// or
[SerializeField]
List<Item> inventory;
// or
List<Item> inventory; 
// plus , in Awake() , if the last one was chosen:
inventory = new List<Item>();

// later, add to the list ...
inventory.Add(itemVariable);
// so, you can make your item first, then add it.

For an array, …

Item [] inventory;
void Awake() {
  inventory = new Item[inventorySize];
 }

and now you can assign to any valid index.

1 Like

in lines 54 and 55, it’s not finding the inventory that i have initialized in the editor. i think this “not finding the inventory” is my problem with the other scripts where i try to access the inventory as well.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace CoS
{
    public class InvRightClickButtons : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    {

        public Text theText2;
        public Canvas InvRCMenu;
        private heroInventory inv1;

        public Canvas ExamineCanvas;
        public Text examinetitletext;
        public Text examinetext;
        public Transform examinemove;


        //texts for item equipping

        private void Start()
        {
            //i guess i don't want this line?
            //inv1 = new heroInventory();
            ExamineCanvas.enabled = false;
        }
 
        public void OnPointerEnter(PointerEventData eventData)
        {
            theText2.color = Color.red; //Or however you do your color
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            theText2.color = Color.black; //Or however you do your color
        }
        public void QuitExamine()
        {
            ExamineCanvas.enabled = false;
        }

        public void ExamineButton()
        {
            InvRCMenu.enabled = false;
            // create examine gui
            // set text to inventory[i].description;
            ExamineCanvas.enabled = true;
            examinemove.transform.position = theText2.transform.position;
            // DEBUG: not finding the inventory
            examinetitletext.text = inv1.inventory[Globals.invindex].objname;
            examinetext.text = inv1.inventory[Globals.invindex].description;
        }

        public void EquipButton()
        {
            InvRCMenu.enabled = false;
            //get gui text and update it.  need inventory[i].objname
            /*
             * armor
             * weapon
             * shield
             * belt
             * hands
             * right ring
             * left ring
             * neck
             * feet
             * hands
             */
            //must interface with state machine and combat system
        }
 
        public void DropButton()
        {
            InvRCMenu.enabled = false;
            inv1.Inv_Drop(inv1.inventory[Globals.invindex]);
        }

        public void CancelButton()
        {
            InvRCMenu.enabled = false;
        }
    }
}

i’m assuming i’m doing something wrong when i try to access the inventory. anybody know what i’m doing wrong?

i have like two other scripts where i use an object to reference a heroInventory class function, and that seems to be where the null reference is coming from. i thought since arrays were a “reference type” using objects would refer to the same inventory?

edit: in this code, line 80 is also a problem, as you would expect.

Where is inv1 assigned? It’s private…

1 Like

okay, i can make it public, but what should i drag into the slot? or should i use getcomponent or something? kind of confused.

edit: i’m confused. i think i’ve been doing this wrong in multiple scripts. still not exactly sure what i should do, or why what i’m doing is wrong. got to grok this lol.

Well, I don’t really know/remember your entire design… I see some of your posts and try to respond if I think I can help… Gotta get the reference from somewhere, somehow. :slight_smile:

1 Like

will this work? i have to fix a lot of things before i can test again.

inventory1 = hasinventoryattached.GetComponent<heroInventory>().inventory;

where inventory1 is an object of Item[ ] type and hasinventoryattached is the GameObject with the inventory script attached. will that get the inventory from heroInventory?

edit:
i also tried, so my heroInventory objects would get the same inventory, the following:

inv1 = hasinventoryattached.GetComponent<heroInventory>();

so far none of this is working for me, i now have even more null reference exceptions.

Sorry it’s not working for you… not sure what’s wrong. I imagine either of those 2 snippets you wrote could work. If you wanna attach a small sample package, maybe I can help more by looking at it…
Otherwise, not sure what to say. Maybe if you showed more code something might stand out.

i forgot to actually add the game object to the script in the inspector. just got home from my friend’s house. will give it another test now, after having dragged the objects into their slots!

Right… That could help, cool :slight_smile:

1 Like

okay, drop still isn’t fully working (the item quantity is successfully reduced by one but a ground item prefab is not successfully instantiated with the item attached), but the stuff that broke is working again (picking up items for example). and now since i put the inventory initialization in awake, it should work and i shouldn’t need to initialize from the inspector, right? hard to tell what it’s doing, i could tinker around and find out i guess.

edit: i’m getting an error on the grounditem script because one of the public variables on the script “does not exist anymore” - that’s because it’s a prefab, and i assign it from the project window. also when i have an item to drop, it changes the actual value of the prefab in the project window, not just the instance i’m dropping. so this needs some reworking. any ideas are welcome, this might require some restructuring of my code but i hope not. progress has been made, that’s what i tell myself, i may be stuck but i still got some things done!

Dunno… glad you got it mostly fixed & working.

Your ground item sounds familiar, but honestly I don’t remember the specifics about what that was :slight_smile:
Give it some time, maybe you’ll sort it out…

1 Like