HOW TO MAKE AN INVENTORY WITH UNITY & C# - BEGINNER TUTORIAL - 1/2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to black thorn prod I know now this is episode one of a new tutorial series on my channel in which I'll show you how to make fruit scratch a cool little inventory system using unity and c-sharp you can easily use and implement into your RPG survival game or whatever other projects you're working on in me for an inventory as you can see I have a little dwarf character the Commuter on the scene and pick up items he comes across which are then added to his inventory he can then use the items in his inventory for example we can consume a health portion or some food equip a weapon or cast a weird magic SunPower he also drop an item by simply pressing the little red cross so as you can see this is pretty cool stuff and the best part is it's very easy to make so with that said let's get started so for now I have a very simple scene setup with my dwarf player character that can move on this cartoony environment there's also these items lying about that I wanted Worf to be able to pick up use and of course check out from his inventory if he wants but before doing that we need to create some simple UI so I'll head over to create UI and choose image and I'll choose my square inventory sprite I made in Photoshop which will basically act as a space slot in the players inventory you can use to carry some item and I'll call it slot 1 and so I'll just place it at the bottom of my screen so it doesn't get in the way from what's going on in game now note that if you've hardly ever used unities UI before I highly recommend you check out this detailed beginner guide I made on the topic with that said I'll create another UI image called cross 1 and choose this little red cross for the image of sprites are then placed here the player will be using this to drop whatever item is in this particular inventory slots awesome it's now time to do some simple programming so i'll create a new c java script called inventory and drag and drop it onto my player character opening up inside of Visual Studio here I'll make two arrays one of type bool called is full we using this array to check whether there's already a night sand inside of a given inventory slot or not and the other will be of type keep object and I'll call it slots be using this to appear in the item we pick up to a given inventory slot which will help us later on place the items nicely in the center of each inventory slots as well as drop the item with that doll now head back into unity and add for now only one element tonight is full array since we for now only have one inventory slot now leave the Box unchecked because obviously when the game starts this slot is not for now also add one element to my slots array and I'll drag and drop this slot one object great it's now time to give our player the ability to pick up these items so I'll create a new C JavaScript called pick up and add that to my health portion pick up for example in its ultra a private variable of type inventory called inventory and in the star function I'll say inventory equal to the inventory component attached to the game object with the tag names player obviously we'll add that tag to our player very soon now I want to check when the player character collides with this pickup indicating that it's time to add it to the inventory to do so I'll crates the ultra enter 2d function that takes in a Collider 2d parameter called other and now I can simply check whether what they pick up is collided with has the player tagging it that's the case we want to add this item to the players inventory so first of all I'll need to check if the inventory has some empty slots or if it's already full of items to do so I'll make a for loop double press tab on my keyboard to automatically generate these syntax for that loop and run this loop a number of times equal to the number of slots in the players inventory now I'll check whether the inventory dock is full of index I is equal to false and it has the case awesome that means the accent can be picked up and added to the inventory so to recap this entire variable starts equal to zero and so I'm checking whether the inventory slot of index 0 is full or not if it isn't then we can add the item to that slot and then we'll type break to stop this for loop as well as states that this inventory slots is indeed full now if not the loop continue i will increment itself off one i will check whether the inventory slot of index one is full or not and so on with I said I don't need to instantiate the item at the correct inventory slots position ah basically be instantiating a UI button with the image of the axe and we just picked up and upon clicking on that button we'll be able to use it in the case of a health potion for example clicking on the health potion button will result in an increase of the players health as well as spa niku little hearts particle effects for example so I'll create a new UI image called health button and drag and drop the health sprite inside that empty slots I'll now add a buzzing component to this image but for now we won't suite any of its settings or add any on click event to it I'll just make sure to reset the transform position of this button they'll simply turn it into a prefab there are head back to our pickup scripts here we'll make a public game object variable called item button and we'll instantiate that item button and the first inventory slot that is already full for a second parameter of this instantiate function instead of stating what position I want to instantiate my button actor I can simply state that I wished this button to be parented to my inventory slot of index I this will basically make sure that my UI button spawns at the same position has my inventory slots graphic now type false because I don't want my button to be in world coordinates since we're working with UI here lastly I'll destroy this pickup since the player has picked up and added it to his inventory so obviously we don't want to still line around the scene and there we go now I head back into unity giant off my health button prefab inside other empty item slots and make sure to add a 2d circle Collider to my portion pickup set to trigger also make sure that my player character has a 2d box or circle Collider as well as a 2d rigidbody + adds to the player the player tag this way the collision between him and the portion will be detected and the player will pick up the item and add it to his inventory so I'll hit play collide with my portion and pop you'll see the portion item appear Indian matauri and later on only to do is add an click event to this portion button getting the portion to do something for example give the player an extra health points if I press pause and take a look at my hierarchy you'll also notice how this health portion button is indeed parents into my slot one image so all I need to do when hitting the cross is destroy all children of that slot one image and instantiate Weber actin must destroy this way back into the Seaview to simulate the player having dropped an item from an inventory now before wrapping up this first video I'll create a few more inventory slots simply selecting the slot 1 and cross 1 objects and duplicating those and then moving them apart and I'll repeat that two more times I'll not forget to set the is full array to four elements instead of one and then I'll giant drop in order the new inventory slots inside of this slot Arabia and now if i duplicate this health portion several times I can pick each one up and you'll see that they're added to my inventory you also notice that when my inventory is completely full I can no longer pick up any item lying about on the grounds because it's fool is always equal to true either that's it I'll now turn this health portion pickup into a prefab and that will by the end of the video we've made some great progress today but we still have laws to do it namely allowing the player to drop items by pressing the little Red Cross collapsing and displaying the inventory as well as get each item to do something when we click on it in the meantime it would be great if you practiced make me more items to add to your inventory because it's really easy to do simply drag up another sprite inside the scene view I'll call this one food pickup for example then add a 2d circle colliders so collisions are the Texas as well as the pickup scripts here instead of try and dropping the health button instead want to drag and drop a food button so I'll create a new food button following the same steps seen earlier turn it into a prefab and then drag and drop it inside that buying item slots and there we go right collide with my chicken leg okay chicken leg item popping up inside of my inventory with that said I hope you enjoy the tutorial and we're able to easily follow along if you encountered some annoying coding Bugaboo you can't solve join the bgp discord server ask for help over there and i or some other developer will try helping you out if you're having trouble with the UI again I urge you to take a look at my UI 101 video or just ask for help be a discord or in the comments ok thanks so much for watching if you enjoy the content hitting the like and subscribe buttons would be so appreciated and encouraging you can also consider supporting me financially by a patreon like these top supporters alright see you in a few days for episode 2 of the inventory tutorial series [Music] [Applause] [Music]
Info
Channel: Blackthornprod
Views: 233,950
Rating: undefined out of 5
Keywords: blackthornprod, gameDev, unity, tutorial, noaCalice, c#, programming, art, animation, inventory, tutorial series, episode 1, #1, HOW TO MAKE AN INVENTORY WITH UNITY & C# - BEGINNER TUTORIAL - #1, rpg, survival game, don't starve, backpack, carry, inventory system, unity inventory, beginner, easy, indie game dev, how to make an rpg in unity, 2d, 3d, game creation, simple
Id: DLAIYSMYy2g
Channel Id: undefined
Length: 9min 43sec (583 seconds)
Published: Mon Aug 27 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.