Unity 3D Tutorial Part 34: Adding Items To Inventory

hello YouTube YouTube and welcome to our 34th unity3d tutorial and in the last tutorial we basically did a unity skin so we made our items in the my thing not have the default skin so we took away the button texture and made our fun bigger so we can see it and now it’s much better so like I said last time in this one we are going to do I think items to our inventory so that would be really cool and it’s not actually that hard so before we do I’m just gonna show you the loading screen from this tutorial in case I forget later but here it is you’ve got killer croc from batman arkham asylum and arkham city as an easter egg yeah so we got him and i made him into one you’ll notice a lot of our loading screens are the same but what’s point changes the theme so basically says Killer Croc escaped from Arkham Asylum witch’s Gotham City’s prison for all the bad guys and and he was on the run however as he jumped into the sewers the void took him into the realm now he’s it now he is loose in the waters of the realm so watch out so that gives us another AI to put in enemy so he can be swimming through the waters and he may chase you you could under some sound effects it’s not be really cool and what we’re eventually gonna do is cus for all these models like if we go to our own screens we haven’t got Bane and Killer Croc Shang well spider-man Superman Jack we haven’t got any of these in but we’re gonna do is later toys just have one big model session just download loads of muggles stick them all in and then put them around well probably not my computer or something but yeah so what really cool so to start and what we could do is add this item add items script to our inventory GUI but as you can see it’s long enough already and if we add it it’s just gonna get even more complex so the easiest way to do it is just create another script and directly link these two scripts so not by using static variables where any script can access it this one will only be using these two scripts linked together like a walk so if it calls one it calls the other as well so it we’re really easily so so we’re gonna do is create a new script in here it don’t have to do the same place but yeah and we’ll call it inventory add item so and well open this up so the first thing to do is to create a walk between them so we’re gonna do is type in a new create a new variable and we’ll call this one the inventory so this variable be linking to our GUI here and then you put color and then you put the same of the script so our scripts name for that GUI is inventory GUI like so so if we were going to take light into the inspector now we could assign that straight to it but why do that when there’s we’ll just make it link itself because it’s so much easier so what we’re gonna do is type in the inventory so this variable and then we will put equals and get component which I don’t think I’ve talked about which basically whatever script this is attached to unless we specify otherwise which we haven’t it will search for it and then all these are clusters components so we’re about to put get component and then we put what component wanted to get so wanted to get inventory GUI alright so basically setting is inventory greed does it say get this script it says it can only accept this script and you could put scripts like so if you wanted many scripts attached to it but I’m just keep as one so now that well when we run it attach that script inventory giving it all searched our character and then find it and then attach it so see it’s got nothing but as soon as we start the game it will attach it anyway so we really simple and now that we’ve done that that’s gonna stop and Eric’s if you did that I would put that there it was throw an hour later saying you in a continuous loop white you can’t break out off so yeah however another worked on that when we add items we need to be able to change the players amounts textures which we can do because they static but we’ve got no way of getting the items texture because it’s not a static every set its static it throws us an error so we have to now three-way interlock these three scripts that this one won’t be able to go back so this inventory items can go to the inventory GUI and go back but it can only go to the payment Orion can’t come back so the inventory can’t access anything in here so it’s the exact same as we did so we’ll just duplicate that and we’ll call this one and the text just cuz that’s what it’s going to store and we’ll call the script variable player inventory like so so copy that duplicate this paste it in and paste it in so now I’ll sign both of them to each one of these so that’s really simply done we’ve now just linked them to script so now if we wanted to say get a very buff entire inventory we type in the textures dark

and then you in the script you can just call anything you want it private or nonprofit so it’s really simple I just I don’t think you can do private I’m not sure about that but yeah so we’ve linked them to but this inventory GUI has got no way of linking back yet so what we’re gonna do is quickly just link it up so it’s really really simple so what we’re gonna do here is go to inventory GUI yeah and we’ll do the exact same as we did here so we just copy this and we’ll paste it in here and we’ll paste it around the bow here in fact we’ll paste it on top of update boom and so we’ll call this one and adding new item the reason we put it in here is because we’re about to change it so it automatically finds itself this one here needs to be accessed by everything so this is gonna be jumping between functions so if we put it there it can do it let’s say if we were to put it in star it won’t do it but we’re about to put this getcomponent on the end of this so it’s you’ll see in a minute it’s much easier to do so this will equal here this will be inventory add item but now instead the price of a coin would put equals get component and then we put white wants to get if we were to put this up here it will throw an error saying we can’t call get component from there which I don’t know why but it just doors but yeah so we’ve put in there and we only won’t function update to it except that because we’re not gonna be client from anything else so it’s really simple but what we’re gonna do at the bottom of all our code at the bottom of the function update what we’re gonna do is make it call a function in here which will add our new items so in here we don’t leave the update so we’re going to rename this to new item so this will be we the inventory changes it will run through this function we don’t want it winning every second we want it to only do it when we say so so we’ll put it at the bottom of here and we will put adding new item which is this variable da new item function done now them two scripts are interlocked they this one every second will call this bit we don’t want this color in it for no reason so it’d be really simple now that we’ve done that and we can start actually coding it and looking out for when it does collect on the new item so when it clicks new item at the moment it doesn’t do anything so what we can do before we program in that is actually go back to our normal screen so if we go back to our level with all the boxes and level one save at the moment well all we’re going to do is make you if it hits it it’ll search our system and if it finds one a blank screen it’ll pick it up if not it won’t do it so as you can see all these boxes have got nothing in common it can’t be collected so we’ll just quickly open our collections screen we this is just going to be a bit of temporary code so we’ve got four boxes three for duplicate this four times three four and that’s too many I will just write put these together so we know these are together so here what I’ve recorded with called them oak wood I in elsewhere the palm wood and stone wood and so now that was done that and all this code in here is going to be the roughly the same but for now we’ll just get rid of everything except the destroy because we want it to destroy it we don’t need to test it because we know it works so if we were to hit all them now it would destroy it so we’ll keep that there for now because we’ve got to add some code to that from our inventory saying look we’ve just collected something they will add it so go back to your add items and now let’s get programming it says what we want so when we clicked in what we basically want to say this ID has been collected so we’re going to create a simple variable here static variable and we’ll just call this inventory new item added and then this will equal to minus 1 the reason we said 2 minus 1 is because our ID starts at 0 if we the way we’re going to code it is basic said if this is more than minus 1 then do it but if we set it as 0 it’s more than minus 1 and it’ll mess up so we don’t want it constantly running so we’ve done that so what we need now is a way to determine the texture so we need to we’ve linked it to get the two textures but what about if the textures empty we want it to basically save well you can’t use it so we’re just new line icons and call this

VAR blank icon and it’s a texture so in here will sink our blank grid icon and say is it the same as this texture if it’s not searched again if it is replace it if that makes sense yeah I think but we’ll just get started right so now we’re going to do is type in if this item so inventory add item dot this if it’s more than minus 1 so there’s our start of it so it’ll call it if it’s more than minus 1 well no but we’re going to change that so ever you clicked an item it equals that ID it’s more than minus 1 yes so what we’re gonna do is type in well now we need right we’ve got the item ID what’s been collected say we pick up wood so it’s 0 we need to somehow get that texture so we’re going to do is create a variable under our icons and call it the new item and this will be a texture again and in that will store whichever item our texture is so to get this we type in the new item equals so this texture will equal our textures for our play in them Toric’s if you log here we’ve got item texture is our textures for IDs that very well equal so we’ve got it but how do we tell which IDs being collected with our static variable so we’ll copy this and all we basically do is put that in the array so press the hook so now if we clicked an item and that changes that will equal to that ID so we clicked on wood item ID zero Bangkok’s ID zero and then we set make that search set and we do on simple so now that we’ve done that we need some way to search all our grids for our inventory to determine what where it is so it’s simple way of doing this is just above here I’m just gonna make this private so you don’t really change somebody’s except this one so I’ll put that down but above here I’m just going to type in VAR array grid equals 0 so it starts off at 0 this is basically going to search check 0 is it a blank icon no it’s not a blank icon plus this by 1 search again so search part 2 is it a blank icon no part 3 is it found that yes revert to 0 change icon it’s it’s super simple so type if array grid is less than 40 because my maximums 40 put whatever your maximum is there and actually what you could also do is if you go textures dot length here copy this bit change it from 40 and put inventory dark grid did we call it grid grids dot length so now that’ll automatically adjust it to whatever ours is so if we were to say change our grids 250th that would automatically update 50 and so it’s 50 so it’s not basic it gives you the ability to expand so it’s really cool now what we need to do right it’s checking it at the moment so it checks zero we need to somehow say if this icon is blank so we’ll type if the inventory here dot grids and then we want to put this search grid so this ID so this is 0 so if blank grid 0 at the moment equals equals blank icon so if it’s the same text yeah then we add the item yeah so that’s where we’ll add the item however what happens if it doesn’t equal blank say it’s already got wood there so what we’ll do here is tacking else yeah and then we’ll duplicate all of this paste it here so if the items array grid equals blank icon so to say if not equals you don’t put if not where it says where you put your expand like your sin up that common name right that even the more than sign or the double equals all you do is get rid of the first it wasn’t put an exclamation mark an exclamation mark basic means not so if you want to set if it does not equal the blank icon then here what we do is change the array grid to search the next one which we seem to do by a grid plus equals one so it’ll take its total pussied by one then research again because it’s still more than one we haven’t changed it so it’s super simple so now we’ve got a basic search feature but we need to actually add the item as in the item is super super simple and actually we’ve got an area I’ve just spotted alone where if you look here we’ve got gooey content in it if we were to go back now I’ll say error and it pops up it’s loading okay no errors Bri it will throw an error I know what the error is where it says here grids here because grids it’s not just a text you know more

this is trying to set it all of it is a texture which we don’t want so we do is put dot image after every time we’ve put this which we really should have done I think that’s everything yes so that’ll start so now it’ll search for the image and not the text because it all one search detects the image and the tooltip which we added still want so to set the new item texture all we do is type in this again dot image so the that what if a greedy is found what is it and then we’ll set as equals is the new item done that’s pretty much it except if we do it now they will get a lot of books so what we’ll do is set the array grid back to zero done and then we’ll set this one and the new ID of the item what’s being found back to minus one that’s it we have literally just completing an inventory at the moment you’ll see in a minute it only builds it all that it works 100% hopefully and so now that we’ve done that what we need to do is basically make it so when we collect the items it changes this to the right ID and then also in in increases the amount of what we’ve got so if we go back to here we’ve already done it for none of them weight and we’re four we did it oh no right so basically we do it for the treetops that’s right here what we’re going to do is type player inventory dot and then what did we call our amount so we call it playing as amounts and then so what’s the item for a wood oak wood with 0 so plus 0 plus equals 1 that’s it so we’ve set that so we can well do copy that in a minute but what we also need to set below this so every time you inventory creases you need to have two lines to the one it I think it’s a good deal like you get a whole inventory so it’s quite good and so what we need to do is copy this one and then set this to whatever I do this is the only way I don’t think it equals 0 that’s it so a copy this paste it in here paste it in here and paste in here so the second one is palmwood so I’ll set this to 1 and 1 I believe ions to know it’s in 3 3 2 2 like so that’s it so now we test it hopefully no errors fingers crossed no errors right let’s go down and we haven’t actually touched it yet so we need to attach it add item boom there we go so you see we’ve got two non equipped things we don’t equip then just completely leave then and blank icon can set these so we call this one grid icon and then the new icon you just leave blank that’s it so we’ll open up some stuff so you can see what happens as we do it so we could play actually no we haven’t done something go back we need to assign the collection script to all of them boxes so if we go back to our items a quick way to attach it to all it’s just hold shift and click all of them and drag collection on boom done everything one of them’s got collection script we don’t need to worry about them too because we’ve disabled the audio so if we were to click Play Now and while it’s playing click our character and what you can see it’s always a matter that loads of things loads of the two scripts it boasts has character because it’s just accessing through our character but now if we were to walk into the one of these items press I so that success we have not set our skin first we need to set our skin first else we can’t see nothing and by the report inventory put it in hood skins boom there we go now we have a fully working one fingers grass let’s try it 20 minutes long else not bad we’ve done good so the grids are still not there why not so we’ll go back to inventory gariac lifts our grids because we reset it so for to set it to one text we’ll leave blank tilt it will leave blank here we’ll put grid set back to 40 because we changed it to element but we need to make it update itself but we’ll do that next time so as you can see oh it takes just for our inventory water that’s it here a sign boom right fingers crossed we’ll collect the ward first and we collect it wait inventory quite as you can see we’ve got one would take again two or three word for word there we go let’s collect our Birchwood let’s go see Oh would hit the new items be enough to

quit so we cut it boom boom as you can see watch the array good reads we catch it I hope it’s fast enough click one to see it searches it actually searches through our grid so we can collect every single one press see the chart no thank you in fact yeah we can test that in a minute there we go so we’ve just collected every single one look you can’t do a no way on yet they’re stationary but it we’ve got a working inventory we’ve now got a forward for page wood and that’s it so what we’ll do just to finish the toil off is open up our tree chart script and find where it plusses it so here as you see it adds one to the inventory so if we just copy this from our collections so what’s it I delete saddening bearish would went to zero that’s it we’ve done I didn’t even follow we’re cute on YouTube yes so that took a lot of tries and while this loads max play reload so in the next tutorial I will be out making it so if it’s already got wood there or it’s got something there it’s automatically increased the size so instead of just saying one wood collected it’ll go one two in the same spot three in the same spot for anything part but what we can also do is walk up this tree press see chopping watch our inventory and ooh we’ve just chopped a tree we’ve collected wood so it’s really really simple it’s not the best inventor in the world but it works what’s to complain about so all good so thanks for watching please like and subscribe and we’ll see you next time