Unity Inventory Script Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi unity game devs this is the second part of the inventory tutorial in which I will add the c-sharp scripts for the inventory first let me explain the architecture of the inventory we are going to create we will add an empty game object called inventory and attach a c-sharp script with an inventory class this contains a list of objects that implement the interface I inventory item and it raises events when an item is added or removed we have to use us for this inventory the player and the inventory UI the player adds or removes items from the inventory this is done by calling the methods add or remove item and the HUD is notified when these events occur and can update the inventories UI ok this is the architecture now let me show you the result first when I start the game we can see how this looks like the player collides with the X which is an inventory item this is added to the inventory and D UI is updated so now let's see how this is implemented here we have the empty game object named inventory with the inventory C shop script attached this class manages a inventory items so let's first have a look at this interface each class that implements this interface like the X has to have a name and image property and a method on pickup that is called when the item is collected by the player the inventory will raise events this is why we have an inventory event arguments class which is a kind of wrapper around an inventory item and used as a parameter when an event is raised and now let's come to the inventory class it has a fixed number of slots a list of these inventory items and a method to add an item to this list there's also some Collider logic that I will point out later on but when the item is added to the inventory which only happens if there are free slots available the pickup method of the item is called the event item added is raised and all the subscribers for this event are notified okay the players able to add items to the inventory in my implementation when he collides with them so he has a reference a global variable to the inventory script in his player controller class now when the players controller collides with a Collider of an object a check if it has an eye inventory item component attached and if this is the case I add this item to the inventory all right now let's have a look at the X and how we convert this into an inventory item I added a class named X to it and this class implements I inventory item so it needs to properties name and image and the method on pickup in this method I just deactivate the game object but this can be improved later on for example to add logic like adding the X as a weapon to the player or something like that okay and the last thing we need to do is to update the UI of the inventory when an item is added and this is done in the hot script that also has a reference to the inventory object so here's the script with the public member variable of type inventory and in the start method I bind the item added events to be notified when an item is added to the inventory and in the event handler I find the inventory panel then a loop through the slots to find the free slot which is not enabled and assigned the sprite of the added item the to do is here to store a reference to the item which is needed when we will actually use it but this will be a topic for the next tutorial ok the very last thing that I want to show you for this part is how easy it is to add a new inventory item like this red gem here to the game I create a new C sharp class call it gem and attach it to the gem object by dragging it to the inspector like this then I open it in vision studio and as you can see it derives from moral behavior and what I do now is I implement the interface I inventory item visual studio gives us the hint that we need a bunch of properties and methods to be implemented I choose implement and add the required properties at the method on pick up we also need a public sprite variable that we can assign in the unity editor for this item in the on pick up method are also just deactivate the game object and we don't need start an update so I'll remove it okay back in the editor I select the gem and add a simple box Collider to it for the GM image I created this one here turn it into the sprite and then assign it to the image of the gem script and we are done and can use this new item for our inventory just like the ex okay guys this was the second part of the unity inventory tutorial stay tuned and don't forget to subscribe to my channel to not miss the next one I will upload the whole project to my patreon when it's finished and it will be available for free thanks for watching this and thanks for supporting me and I hope you come back soon to Jana
Info
Channel: Jayanam
Views: 77,808
Rating: undefined out of 5
Keywords: unity inventory tutorial, unity inventory system, unity inventory scripts, unity c# inventory, unity c# architecture, unity c# scripting, unity c# tutorial, unity c# programming
Id: Hj7AZkyojdo
Channel Id: undefined
Length: 5min 42sec (342 seconds)
Published: Thu Jan 04 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.