Need help with Item system - Basic problems with scripting/prefabs/inheritance/references

Hey!

I’m making a basic 3D game right now, and I’m trying to implement an item system. My player gameobject works fine, but I keep running into roadblocks when trying to figure out how to implement items. I’ll explain my current thought process, and I would love some help either fixing the way I’m thinking, or suggestions for other ways to implement items.

What I’m currently scripting and have planned:

A generic “ItemContainer”, which is a small bouncy physics cube that can have an ‘item’ script object attached to it. I wanted to be able to add any new custom item script (axes, potions, stones, etc. each with their own overriding UseItem() method, all inheriting from an ‘item’ superclass) I made to a generic container without needing to model/make/save a new custom prefab game object for each new item I want to add to the game.

My Player has a reference to an equipped ‘Item’ object, and can pick up (finds the closest ItemContainer, grabs its Item script, and removes the ItemContainer from the scene) and drop items (instantiate a new empty ItemContainer, add a new Item script to it, and clear the player’s equipped reference) from and into the surrounding environment. Players can use an equipped object, which calls their equipped item’s UseItem() method.

Issues that I’d love help and suggestions to either fix or get around:

  1. Everything with ItemContainers is working well, but it’s difficult to utilize new prefabs (hitboxes, projectiles, particles, etc) in each custom item’s UseItem() method because the Item script default references (dragged into the script in the editor) to required prefabs aren’t applied when the scripts are added as components to the ItemContainers at runtime. I wanted to make a floating empty ItemManager gameobject that would store all the required prefabs and could serve them to items as needed, but I can’t store a reference to the Manager in the items (for the same reasons as above), and searching the scene for the ItemManager every time an item is used is supposedly extremely inefficient.

  2. When picking up items from the ground and the ItemContainer is Destroyed from the scene, the item script that was attached to that container is also destroyed, which means the player can’t “use” it (the reference is ‘missing’ during runtime). I want to be able to make a copy of the item script, but I don’t really know how to go about doing that with inheritancy involved without just making a massive growing switch statement that needs to expand every time a new item is made.

I think I’m going about this all wrong: (unity doesn’t seem to be equipped for passing around c# objects from gameobject to gameobject) does anyone have any suggestions to either get over the issues with my current approach to items, or another approach to items entirely?

I like to separate the concept of displaying an item sitting on the ground in the game world, from how it is used by the character. If an item is sitting on the ground to be picked up, that is unlikely the place where you need the UseItem() code implemented. The item sitting in the scene likely doesn’t need to know at all what it can do - just what model to display and some information needed just to fit it in inventory (what inventory slot it is allowed, what weight it is if you limit your inventory based on weight, etc).

When the item is in the inventory, that could be another entirely separate prefab, or it could just be a C# object, but that’s the one that will need to know how it is used. And that is where you would hook up your UseItem() code, not the object sitting on the ground.

Passing C# objects around between GameObjects only doesn’t work if you destroy the GameObject that held the only reference to the C# object. If you hand off the C# object reference first, then you should have no issues.