Unity Farm Game Tutorial - Loading Resources - Part 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there it's me silverley b and today i bring you the part 4 of our little farm game tutorial today we'll be looking at how to load resources and how to order them this part is kind of an extra part because as we saw on the last tutorial the game is functional right now so if we hit play we can buy corn we can buy sunflower and we can buy tomato as well but the thing is right now we have three plants but in your own farm game you might want to have more plants than three maybe you have a variety of plants and right now whenever we want to add a new plant we'd have to first go here and create a new plant object for instance i'm going to create a super corn out of the corn game object so i'll duplicate it and right here i can name it super corn we're gonna be changing the time between stages to three and the buy price to 40 and the sale price to a hundred we can also rename here choose super corn and okay we've created the new plant object now we have to duplicate this plant item and here instead of the tomato on the plant we just drag and drop super corn and when we hit play we can now see that we have a super corn that we can buy and it's okay if you want to build your game this way where whenever you want a new plant you just duplicate the plant item and edit here but another thing we can do is we can start using resources and instead of loading each plant item we're gonna tell unity to do it for us the way we do it is first let's pause this and on the scripts folder we're gonna create a new c-sharp script we're going to call this one store manager we can open it up so here on our store manager script what we're going to be doing is first we're going to be loading all of our plants and then we're going to be putting them on our store using our plant item prefab that we've made so first we need a reference to the prefab so i'm going to be asking for a plant item so here's where we're going to be putting our prefab and we're not going to be using start or update we're going to be using awake so here on our awake function what we're going to be doing is first we're going to be loading our plants so here in our load plant sorry load plants it's where we're going to be loading them and what we need here is to use resources dot load all okay and here we can see that it requires a path and our path will be plants our plants folder and we're also going to be using a type our type is going to be plant object so we just need to say type of plant object okay this is our scriptable object class and this is our biopath one thing that needs to be noted though is that here that we have our plans path this is not going to be accessing our assets slash plans no this is not the folder that it will be accessing it will actually be accessing the folder assets resources plans and currently we don't have this folder so when we go back to unity we'll need to be creating it okay and now what we need to do is we need to instantiate our plant item in order to do so we're going to go with a four each and here instead of item we're going to be using plant it's just the name of the variable and here instead of collection we're going to be using load plants this means where we loaded our resources when we create our plant item we need to store its reference to the plant item class to the component that it has so in order to do so we're going to create a plant item called new plant and here we're going to first instantiate our prefab so instantiate plant item and here we can use the transform parent as this transform right now it's giving us an error because this is a reference to an instantiated game object it's not a reference to a plant item in order to fix it the only thing we need to do is to get the component plant item out of the object okay so here this way it's correct and the last thing we need to do is here on the new plant we need to set its plant as our plant object okay so here all we need to do is new plant dot plant equals two and right here we need to cast our plant so plant object and plant okay this is looking good we can save it and we can also make the adjustments on unity in order for this to work so going back to unity we can delete this and here on our plan store we can add the store manager our store manager asked for a plant item now we can select here plant item so the prefab is okay and at last to work with the resources we need to create a new folder and call it resources okay make sure it's spelled correctly and we can drag and drop our plants to here so right now here we have our plants and when we hit play this should be working so yeah when we hit play it loads here as we can see it's working correctly we can buy our plants but the thing here is we had an order going on on our plant store that was from the cheapest plant up to the most expensive this means that the tomato came before the super corn and this happens because here as you can see on the folder it's organized according to alphabetical order we can fix this in a few ways as i said it's organized in alphabetical order so we can just rename every single one of these for instance give corn a number of one first and sunflower rename it with a two super corn rename it with a four and tomato rename it with a free so right now after renaming every single one of these we have them in a order from the cheapest to the most expensive and we can keep using this order as we go along and we can even instead of just using the using a single number use the price from each of these objects for instance we could use instead of one use 10 for the corn and we could use this for every single one of them so it is a solution to our problem of loading these items but the thing is we'd have to be careful so whenever we change a price we'd have to remember to change the name as well and if we want to sort by something else for instance the time between each stage or if we want to sort it by the sale price then it's not going to be working we'll have to rename every single one of our items and that can be costly especially in time because you have to go through every single one of these or you know make a script that can do that for you and i could teach you guys how to rename assets through code if you'd like to see that leave a comment down below but right here the solution i'm going to be using to order all these plants is going to be through code and right here on the store manager what i'm going to be doing is i'm going to be keeping a list of all of our loaded plant objects and i'm going to be using that to order the plants how i'd like them to be ordered okay so this here i'm going to create a new list of blend objects i'll call them plant objects okay nice name and i'm going to initialize it and right here i'll just comment this part and i'll just add this plant into this list so in here we can do and we need to cast us just as we did right here so we need to save the plant object and inside here that's it but outside we're going to be needing to sort this list in the order that we want it in order to sort this we're going to create a sorting function so down here we're going to create a new function that's going to return us an int so it's not going to be void and it's going to be sorting by price it's going to be receiving two plant objects so plant object 1 and plant object 2 and they both are going to be compared to each other in order to do so we're going to be using the plant object 1 dot by price and here we are choosing to sort it by price so if you wanted to sort it by something else for instance if you want to create an index or an order to each of your plants here's where you choose what we're going to be sorting by okay so it's going to be by price and i'm going to compare it to to the plant object 2 dot by price so this is our sorting function in order to sort our list we just need to say plant objects dot sort and here we can use our sorting function for instance i could copy this and paste it over here call it sort by time and instead of my price here i could just replace it with time between stages okay great and if we wanted to sort by time we could use this function right here okay so we have sorted our list now what we need to do is to create the plant items so it's just going to be this again so we can use our forage and this time it's going to be plant and instead of load plants this time it's going to be our list so plant objects we can cut this from here and paste it down here and not comment it anymore now the only thing we need to fix is there's no need for casting and this should be working so again this function we are not using it this was just an example of how you could sort it so we can save this and go back to unity oh i hadn't hit stop so now we can hit play and check to see if this that is a mess right now so corn is the last if this is going to be working fine so here we can see it's all sorted by the cheapest to the most expensive [Music] so that was it for today guys thank you so much for watching and if you like this video please leave a like if you have any questions you'd like to ask leave a comment down below i'll try my best to answer them and if you want to stick around please consider subscribing that was it thanks for watching bye you
Info
Channel: SilverlyBee
Views: 533
Rating: undefined out of 5
Keywords: unity tutorial, unity, game, game dev, games, tutorial, game tutorial, farm game, farm sim, farm game tutorial, game development, planting, harvesting
Id: -VRqK-ebz6U
Channel Id: undefined
Length: 13min 24sec (804 seconds)
Published: Fri May 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.