Implementing the inventory system proved to be a far
larger challenge than I had anticipated. First off, I had no idea where to
start… After doing a bunch of google searches I realised that the code I was
about to deal with just shot up in difficulty from beginner to intermediate.
Suddenly I was looking at loops and wondering how to make head nor tail of it
all. This is when I stumbled upon
YouTube
user
GameGrind, and his
playlist
on an inventory system.
After tweaking and playing with the fully built system,
there were still parts of it that I didn’t understand, and there’s no use
implementing a system that I don’t understand cause after all, if I want to
modify it in the future then I’m not going to know where to start. This however
did give me a much greater understanding of loops, and how object data is
stored.
Using this knowledge, as well as a helping hand from YouTube user Brackeys -and his tutorial on scriptable objects, I was able to put
together an inventory system that worked, one that I fully understand and one
that I am happy to move forward with. One of the issues that I ran into was
with the UI system. It felt strange clicking play and seeing the inventory,
already open. To change this, I disabled the objects in the scene using gameObject.SetActive(false) although
this issued an error. It turns out that this completely disables an object,
therefore any scripts attached to said objects cannot be accessed. I did
however find a workaround, thanks to Unity Forum user akingdom. The workaround was to add a canvas group to the inventory
UI with an alpha of zero and making it noninteractive. This means that anything
tied to the canvas group would be fully transparent and couldn’t be clicked on.
The short version of how my inventory system works is
that I create a scriptable object of the ore that I want, attach it to the ore
prefab and then when mined, the object will be added to a list along with all
the data that the object holds. This means that at any given time, each slot in
the inventory can be accessed, along with data such as quantity, whether it
stacks with other objects etc.
The game now also contains a working UI system that the
player can open and close, keeps track of how many items the player has can
restricts the amount that they can hold. In addition, I added a cash variable
and display ready for the next major update: the shop.
In its current state, the Player can mine ores but has
nowhere to take them. This leads the next stage of development to a shop where
the player can trade in their ores to purchase equipment upgrades.