# Trouble with Inventory System and Item References

I'm currently trying to get a simple inventory system to work but I'm having a lot of trouble.

This isn't the exact code I have, but it's a shorter version that I'm pretty sure would function the same:

public struct ItemSlot {
public Item item;

public ItemSlot(Item item) {
this.item = item;
}
}

public class Item : MonoBehaviour {
public string itemName;
}

public class Accessory : Item {

}

public class Inventory : MonoBehaviour {
public ItemSlot[] itemSlots = new ItemSlot[1];

public void AddItem(ItemSlot slot) {
itemSlots[0] = slot;
}
}

public class Player : MonoBehaviour {
public Inventory inventory;

private void Update() {
if (Input.GetKeyDown(KeyCode.E)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;

if (Physics.Raycast(ray, out hitInfo, 20)) {
GameObject target = hitInfo.collider.gameObject;

Destroy(target);
}
}

if (Input.GetKeyDown(KeyCode.G)) {
print(inventory.itemSlots[0].item);
}
}
}


For a bit more prefacing I have a capsule object with the Player script on it, an empty with the Inventory script on it, which is then attached to the Player script (I'm not sure if I want to make the inventory a scriptable object which is why I've done this), and then lastly I have a cube object with the Accessory script on it.

So, when I press E and pick up the item it gets added into the player's inventory and then destroyed from the scene. Then, when I press G to display that item in the inventory the console just says null. Not Null, which is what an actual null would show up as, but the string "null". If I use visual studio's debugger and have a look at what the accessory's base is, it also says null, but for some reason if I then try and print the accessory's name then that works fine, even though that variable is defined in the Item class.

If I instead do this in the Inventory class:

public void AddItem(ItemSlot slot) {
ItemSlot[0] = slot;
print(ItemSlot[0].item);
}


It works fine and prints Item (Accessory) in the console. Even if I print it right after I destroy target in the Player class, that also prints Item (Accessory), so I have no idea why it doesn't work on the next block of code.

Sorry this is so long and maybe an easy fix, but I'm lost.

Edit: I think everything should be fixed now.

• It looks like you might have over-simplified the version of the code in this question. The version of AddItem you've shown us takes an ItemSlot as an argument, but you're calling it with an Item. And your Player type lacks an itemSlots variable to use in your call to the print function. So this code will not compile as-is. Can you try placing this code in a new, empty project, so you can make sure it compiles and serves as a Minimal Complete Verifiable Example of the problem? – DMGregory May 6 at 17:33
• When you get the string "null" printed out, that's a sign that you're trying to reference a UnityObject that has either been destroyed, or was never attached/assigned in the Inspector. For those purposes, Unity inserts a fake null that can give you more instructive error messages if you try to use it. – DMGregory May 6 at 17:38
• @DMGregory Sorry about that, I typed the code on here so I figured there would be some errors. It should all be fixed now, and I also went and created a project with this code like you suggested and it compiles and produces the same null error. Thanks for the link as well, I'll have a look at it tomorrow as it is quite late for me now. – Genzou May 6 at 18:20

• Oh, right that makes sense, I just figured it would instead store the information. However, why does it still return Item (Accessory) directly after the object has been destroyed? Anyway, could you provide an example of an archetype data structure as I haven't used anything like that before. – Genzou May 6 at 18:57