I’m trying to make an Inventory system for my game but I have some trouble figuring out how to instantiate an item.
My first idea was actually quite stupid. I just store the GameObject in the InventoryItem class when I collect it. Guess what, null reference exceptions… And of course, this was expected to happen.
And I actually did a lot of work after that, even created some quests that use the inventory system, and now I have to change a lot of it which means I will have to make some refactoring to the quest scripts as well…
Now I’m thinking to create an InvenoryHandler GameObject (that is gonna live for ever), give it a script with an array of GameObject, and assign each inventory item prefab in there. Now every time I want to instantiate a new item, based on its type I can grab a reference of the GameObject from the InvenoryHandler script and use it to instantiate the item.
For my game this might be good enough but what if I’m creating an RPG with 5000 different items? Won’t this make my scene to perform quite bad?
I’m not really familiar with how Unity actually handles these prefab instances (the ones that you assign through the inspector I mean). I know that they don’t appear in or even in the scene hierarchy when I play the game.
I’m not sure if the Resource class can help me**.**
Thank you for your time!
The answer is always the same… ALWAYS. There is NO MYSTERY here. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
- also known as: Unassigned Reference Exception
- also known as: Missing Reference Exception
http://plbm.com/?p=221
The basic steps outlined above are:
- Identify what is null
- Identify why it is null
- Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
https://discussions.unity.com/t/814091/4
Step by step, break it down, find the problem.
If each item is 100 bytes should be fine. If each item is a collection of 4096x4096 textures, not so much. As always, you can do a LOT of something if it is small and easy, or you can only do fewer of something if they are big and hairy.
1 Like
Thank you for your answer! By the way, I know exactly what causes the null reference exception, but the problem is that the culprit is mainly the way I’m storing the GameObject in my inventory system, which I believe (and it makes sense) is a bad thing storing references of GameObjects in different locations in your code.
I always need to have a GameObject reference each time I need to instantiate a GameItem, so what do you think:
Should I Load it from the drive and instantiate it
OR
have it directly inside the game (The InventoryHandler that I mentioned) and use it in Instantiate()?
This can mean many things:
-
refers to an in-scene asset that you are going to use (possibly always, possibly only when you need it)
-
refers to an in-scene asset that you are NOT going to use directly, but rather will make copies of with Instantiate
-
refers to an on-disk asset that you must first Instantiate into your scene
In all cases, Instantiate will return a reference to the newly-created thing it made for you.
Keeping references to things on disk is lighter on memory, but at some point to use it, it has to be instantiated, at which point it’s no different than just being in the scene.
Depending on the intended lifecycle and use cases, choose appropriately above.
1 Like