Unity Farm Game Tutorial - Scriptable Objects - Part 3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there it's me silverly b and today i bring you the third part of our farm game tutorial this was supposed to be the final part and to be about scriptable objects and loading resources but since there's a lot of the gameplay aspects on this tutorial i've decided to make this one about scriptable objects and make a final part 4 about loading resources so on this one we'll be covering scriptable objects and also how to plant how to make the store and how to deal with the money of the game since it's a lot of subjects i decided to also divide this one by chapters to make navigating this tutorial a bit easier and it can be a bit overwhelming for beginners but if you have any questions you can always leave a comment down below so for starters let's talk a bit about what scriptable objects are scriptable objects are used as data containers they are normally used to store unchanging data in order to reduce your project's memory usage so on this project we'll be using scriptable objects as a way to store the data of each of our plants for instance the corn is always going to have the same name it's always going to have the same price it's always going to have the same icons and the same plain stages so what we do with scriptable objects is we store all this data and then we access it through script and this is what we're going to be doing today for starters we're going to open up the plot manager script and as you can see we have here a few variables that are related to the plant that we are going to be planting in this plot of land now what we intend to do with the scriptable objects is to make a plant object that is going to hold all the variables related to the plant that we're going to be putting on the plot so what we need is we need the planche stages this means the sprites that we're going to be using we also need the time between each stage and for now we're going to be working just with that just to check this out so let's head back to unity and over here we can go to create c-sharp script and create a plant object script when we open this up the first thing we're going to need is we're going to change from mono behavior we're going to be deriving from scriptable objects and in this case we're not going to be needing a start or an update function as we saw over here on the plot manager we're going to be needing the plant stages so we're just at this here and also the time between each stage we're going to be making it public and we won't have a default value another thing we're going to be adding would be the name so it's going to be a string called plant name we'll not be using this on the plot manager but we're going to be adding this to the plant object already just to use it later on another thing would be to create in the create menu this type of object in order to do so we're going to use the create asset menu and we're going to say that whenever we created the file name of the new plant is going to be surprise new plant and also on the menu it's going to be called plant we can identify it by selecting plant what this means is if we head over back to unity we can check it out let's create a new folder for the plants that we're going to be creating so let's go to folder we're going to be naming it plants when we open this up we can go to the create asset menu and we're going to create you can check over here that there is a plant option for us to create this is what we did with the create asset menu line and when we click to create a new plant you can see over here that it has the new plant name so what this is going to be it's going to be a corn since it's corn let's give it the name corn and we can lock this over here so we can get this price for the corn plant so it's here through here and this are the stages of our plant let's drag and drop it over here and we can change the time between stages to be two so two seconds between each stage now what does this mean this means that over here we have the corn as a plant object it holds the data for each plant that we're going to be using and for now we only have corn and over here if we go back to the code we can change on the plot manager instead of asking for a public sprite planned stages or setting the time between stages we're not going to be using this anymore we're just going to be asking for the data that we'll be using this is the scriptable objects so since we created the plant object we're going to say public plant objects selected plant so here we ask for the plant that we'll be using and instead of plant stages we can replace everywhere that we have plant stages with selected plant dot plant stages this means that we'll be accessing this variable that we just did so the time between stages is another thing that we're going to be changing instead of just time between stages it will be the time between stages of the selected plant that we've informed the plot manager so selected plant dodge time between stages we're gonna save this oh sorry i'm gonna just fix this and we can save it and head back to unity to check it out so when we open this prefabs the plot if we open it here and unlock this plant object from here we can see that on the plot manager it asks us of a selected plant so if we go back to the plants we can drag and drop it over here and it should be working fine just as it did before so we hit play and let's check dessert we can go back here to the scene as well it seems to be working just fine as we expected but now if we go over here to the plants and create a new plant we can finally create different types of crops other than corn so let's go with the sunflower and we're going to give it the name of sunflower the time between stages is going to be five so it's going to be slower than the corn and we're gonna lock this on the inspector go right back to the sprites folder and select the sprites off the sunflower so here through here we can drag and drop it over on the plant stages and when we go back to the prefabs we can open up this plot and here we just need to replace the selected plant from corn to sunflower i'll hit play as you can see the sunflower is working just fine so we have here another plant and i'll do the same thing for the tomato plant and you can check it out in just a second as you can see i've created the tomato here and the time between stages is going to be 10 it's a larger time but it's just for show you can adjust it for the time that you're going to be using on your game for how it's going to be working and when we hit play we can check it out to see if it's working as you can see it's working perfectly fine and now we got three different types of plants that we can start selling in our store so let's begin by making our store okay so what we need to do now is we need to build the plant store in order to do so we're going to start by creating a panel we're gonna name this panel store okay so here on the store we can focus on it and first thing we're gonna do is we're gonna head to the canvas we're gonna change the ui scale mode to scale with screen size so here we can change the distance on the left side to 500 okay so here on the store we can also change the color to be a darker one and keep the opacity i think this looks okay and now what we need to do is we're going to create a title for this plant store which is going to be plant star so i'll create a ui text object i'm going to keep it basic and only use text but you can go ahead and use text mesh pro if you want to so i'll create a text i'm gonna name it store title and on it we're gonna change the text to say the store name so it's a plan store as you can see it's quite hard to see it on this color because it's dark and in order to make it easier on the eyes we're going to go with a brighter color so here we have the plant store title and i already know what i want my plant store to look like so i'll be using pre-established numbers and ways of organizing it but you can experiment with those numbers and with the objects to find the plant store that you like the most so here i already know that the width is 260 and i already know that i want it centered and right here on top i'm also gonna expand it and give it a font size of 30. it may not look so good right now but i will be changing the font later on so this is it for the plant title and for the money that you have i'm gonna just use a currency right here on the side so to do that i'll just duplicate the start title so ctrl d okay i'm gonna rename it so f2 and the name i want which is money text okay and i also know that i want it coming from the right side so i'll adjust the alignment on the text to the right okay i also change it to look a little bit more how it's going to be looking in game except it doesn't have any numbers right now because it's still just a placeholder so now what is left to do is to create the actual plant store which is going to be holding each of the items that we're going to be selling in order to do so i'm going to create an empty game object here i'm going to call it plant store and in order to have all the plant items right here in order and aligned i'm going to be using something called grids and here i just look for the grid and i'll be using grid layout group here you can see the cell size this means the size of each cell on the grid the spacing and other configurations right here what i am going to do is i'm going to say it has a fixed column count because i want only one column okay so it's going to be a list of game objects you can create your store using this grid to have many columns of objects but i prefer just to have one column and they will be presented each one in each row okay and here we're gonna be changing the cell size to fit the size of the row that we want so on the x-axis we're gonna want a 260 width and on the y-axis we're going to be watching an 80 height okay so here we have the spacing between each axis and since it's going to be only one column the x spacing is not important but on the y we're going to be wanting a 10 spacing between each row okay so what we need now is also to change the width here to be 260 so we can align it better here okay this is looking great and now we can start by creating each item on the plan store so we're going to create an empty game object called plant item and inside it we're going to be building each of our plants so the first thing we're going to be needing is a visual way of showing which plant we're buying so we're gonna head into the sprites and here we can create a ui image okay and we're gonna give it a width of 80 and a height of 80 and we're gonna be anchoring it to the left side of the plant item so we hold alt and click it here and now it's on the left side okay and the image we're going to be using for the icon background is going to be this one so we drag and drop it right over here to the source image great we can call this the icon background okay and inside it we're gonna have another image which is going to be the icon of the plant that we are using so we're gonna go into ui and use the image we're gonna name it icon and now we're gonna change it to be 70 in width and 70 in height okay so here we go this is what it's going to be looking like we can now drag and drop it we're gonna go first with the corn icon and drag it over here and now in order to preserve the aspect we can check this box right over here this is looking great for now okay so another thing for identifying the plant that we're buying is going to be the plant name so here on the plant item we can create a text and name it plant name we already know what to do we're gonna make it white we're gonna change it to corn we're gonna also anchor it to the right top side of the plant item so hold alt and go over here okay one other thing that we're going to be changing is going to be the font size we're going to go with 30 and we can adjust over here so we can see it looking great and now in order to do the price we're gonna just duplicate this plant name and we're gonna change the name to plant price we're gonna change it to a money sign we're gonna change the font size to a 20 and we're going to anchor it to the right middle of the plant icons so again alt and choose it right here it looks a bit weird and just opposing right now but we're gonna fix it later on and now on the plant item we just need one other thing which is going to be the button to buy the plan that we need okay so we're gonna go with ui button great we're going to change the name to buy button we're also going to be changing the color of the button it's going to be a greenish button and we can change the position of it to be anchored right here on the bottom right corner the width is looking good and now what we're going to be doing is we're going to be changing the font to a different one we're going to be using a pixel font it's a font from google and i'll leave the link on the description to download it and i'll import it as a new asset in order to apply it we're gonna select the store title the money text the plant name the plant price and the text from the buy button and we're just going to drag and drop it into here on the font slot of the text so this is what it's looking like okay so i'm gonna change the text the font size it's going to be 24 okay so it's oh and i forgot to change the text so it's by and it's going to be dark it's looking better now you can also experiment with building a few of the texts i like how it looks right now and also change the alignment on the plant text i think it's okay for now i'll just move it a little bit and i think it's looking okay so now we have here the plant item and in order to do the second plant we can just hit ctrl d and here we have two plant slots we can go here on the plant item and change the icon we just need to go to the sprites select the icon here and drag and drop the sunflower over here and change the plant name to something like sunflower and that's one way to go about it but i personally think that it's a very bad way of going about making your game because right now we have three plants but what happens when you have 50 plants are you gonna be making each and every plant item that you're gonna be needing or do you want the computer to be doing it for you so right now because we just made descriptible objects we can then use the data in descriptible object to populate this plant item in order to do so we're gonna first make this plant item into a prefab and then we're gonna be creating a script that can process the scriptable objects into a plant item right here on our store first we're gonna delete this plant item that we copied and then we're gonna go to the prefabs and we can drag and drop the plant item right over here so now we have here the prefab ready to be duplicated and now we need to go to the scripts and create a c-sharp script called plant item okay so what we're gonna be needing right here is to get that plant item and pass this informations that we have right here you also see that we don't have the price right here but we can change that by going with a public and called price now when we head back to unity we need to set this price to each of the plant objects that we've created i'm going to save this right here and i'll go to the plant item and since we're going to be making changes to ui components we need to use the namespace unity engine.ui so over here we're going to be asking first for the plant object and now we need to access each of the ui objects that we are going to be changing for each of the plants so we need to change the name the price and the plant icon if you look over here on the plant object you see that we also don't have the plant icon we can change it by going public and since it's an image we can go with sprite and as for the icon we can save this and over here on the plant item we can start by asking the ui for the name we can also ask for the ui for the price and at last we can ask for the ui for the image we're gonna put the icon we're not going to be using the update function and here on the start we're just going to be setting these in accordance to this object right here okay so name text dot text it's going to be the plant name plant name and the price text this one is going to be a bit different because we're going to be using a dollar sign at the start sorry a dollar sign and we're gonna add the planche dot price and we also forgot the dot text right here so on the image for the icon we're gonna say icon dot sprite this way we're accessing this product for the image it is going to be equal to the plant that i can okay there's one other thing that we need to do when we click the buy button we need to associate it with a function the function will be right here so public void by plant and we're not going to be implementing it right now but we can show a little bit of text right here so so now we can go back to unity the first thing we need to do is we go to the plants and we're going to be giving each plant a price and also an icon okay so here we go with 10 and the corn icon and i'll do it for each of the plants okay now that the plant objects are fixed we need to go to the prefabs and open up the plant item prefab and we can give it here a width and a height so they don't look too weird and here we're going to be adding this plant item script so here we just need to drag and drop each of these options into here so planche name is going to be the plant the name text plant price is going to be the price text and the icon is going to be the icon and for now we can set the corn to be the default plant and another thing we need to do is to set the function of the buy button so here we're going to be associating it with the plant item script and we're going to be calling the buy plant script okay when we head back over to the scene we can go to the plant store and duplicate each of these plant items and replace the plant object though here we have the sunflower and here we have the tomato plant i just adjusted the screen so we could see better and here when we press play it should show each of the plans that we have here and also when we click buy it should show us that we're buying it correctly okay so here the corn when we click buy it says bought corn bought sunflower and bought tomato this is looking great so far and right now what we need to do is we need to create the buy function in order to for it to work properly because right now it's just going with the tomato that we used last time okay so what we need is whenever we are here on each of the farm plots instead of the selected plant when we select buy it's going to be using the plant and to do that we're going to be needing a few twigs to what we have right now okay so first let's create the farm manager we'll open it and right here we're gonna be handily selecting a plant so i'll go with public select plant and this is going to be a function where we receive a new plant item okay that we're going to be calling new plant and we also have over here a public plant item that is going to be the selected plant and also a bull to keep track of if we're planting or not okay we're gonna go with false at the start okay so here we just need to check if the select plant is equal to the new plant if it is what we're going to be doing is we're going to be de-selecting the plant so select plant is going to be null and we're also going to stop planting so this planting is going to be equal to false otherwise if it's a different plant what we would need to do now is just say that the select plant is going to be equal to the new plant okay and also we're going to be saying that it's planting is true so if we select the same plant that we're currently selecting this makes it deselected and if we select another plant this makes the other plant be selected it's very simple and here on the plant item on the by plant function we're gonna be needing to call this function over here on the farm manager with the plant item reference right here in order to do so what we need is we need a reference to the farm manager so let's head to unity in order to create it so first here on the farm we're going to be adding the script of the farm manager so just drag and drop it over here and here we're going to leave it as is because this is going to be set by the function we just created another thing is over here on the plant store each item is going to need a reference to the farm manager in order to do so we're going to be heading back to visual studio and right here on the plant item script at start we'll first create a farm manager fm and this is going to be getting the reference to the farm manager since the farm manager is only attributed to one object we can do it here at the start we can say that fm is going to be equal to find object of type form manager okay since there's only one of these we can be sure that this is going to be working correctly we're also going to be making a new function here called initialize ui okay and right here we're just going to cut this and paste it right over here and we're going to call this function i'm putting it over here just for organization purposes it's not going to make it such a big difference but i like organizing my stuff like this and another thing that we need is whenever we buy a plant we call the function on the farm manager we call this function so right here we're gonna say fm dot select rent this function and we're gonna just reference to this this means this plant item script okay we can save this and right here in order to check if this is working we're just going to be using a fielddebug.log to print us the selected plant or the deselected plant okay so and this is not going to be working because we are calling it right after we say it's null so that's why it's going to be working and right here we can call it this okay so selected so i'm going to save this and when we head back to unity we can check to see if this is working properly so when we hit play we can click to buy and it says that we've selected corn it says that we've selected sunflower and it says that we've selected tomato and now when we click again on the tomato it says that we've deselected it it seems to be working correctly as we'd expect and now we're going to be heading back to the farm manager script to make the selected plant actually be planted on the farms lot okay so here we have the selected blend and over here on the plant manager we're going to be first needing a reference to the farm manager so here we're going to be having a farm manager fm and we're just going to be saying that it is this transform dot parent dot get component we can do this because we know that the farm manager is going to be set on the parent so this is going to be working okay if it's not set to the parent we can use it just like we did here on the fm okay so here on mouse down we are gonna be needing to check if first if it's planted because if it is planted then on miles down we're gonna be harvesting but when it's not planted we need to check if the farm manager has a reference to a selected plant so f is planting oh sorry fm dot is planting then we can plant and right here on the plant we're going to be needing a plant object okay this is going to have the data that we're going to be storing on this selected plant okay and this selected plan doesn't need to be public anymore you can maybe make it be public but there's no need and right here on the plant we're just going to be passing fm dot select plant dot land so what we're referencing here is the farm manager here and then the selected plant here which is a plant item that has a reference to the scriptable object we're using this so over here whenever we select it we need to pass this new plant into the selected plant so selected flat is going to be equal to new plant we can save this and check to see if it's working so let's head back over to unity hit play and check to see if it's working okay i can buy it i'm gonna be planting corn here i'm gonna be planting sunflowers here and a tomato here okay it seems like it's working correctly but one thing that's not working is we don't have any money and we're not using the price of the plant for anything okay so what we're gonna be doing now is adding money to our game we're gonna be managing the money on the farm manager okay so we head back to visual studio and over here on the farm manager we're gonna be having a public end that is going to be our money and we also need the ui element that corresponds to our money because we're going to be managing it here so i'll use the namespace unity engine.ui and i'll be referencing here the text for the money okay so money text great so first i'm gonna set the default value to a hundred and i'm gonna be creating a new function to deal with our money okay so that is going to be dealing with every money transaction that we have and i'm gonna be needing a value for the transaction whenever we receive a value we're going to be adding it to our money so if your transaction is going to be subtracting from our money then it's just sending a negative value over here so here we can have plus equals value and we can write on the text that our money equals to a dollar sign plus sorry plus our money at start we also need to tell the game how much money we have so here we can say it like that we're also not going to be using update function so we can just delete it i'm going to save it head back over to unity and check to see if it's working so here we can go to the form and it's going to be asking us for a text and for the text we just need to reference the money tax okay so here we go and when we hit play it should show a hundred that we have so yay it's working okay so another thing we need to do is update the plot manager because whenever we plan something or we harvest it we're going to be needing to make a transaction when we plant we're going to be using the price and when we harvest it we're going to be getting a sale price but first here on the plants we don't currently have a sell price we just solely have a price in order to fix this we're gonna be changing this from price to buy price and we're also going to be adding a sale price that is going to be telling us how much we're going to be getting when we sell this plant okay so let's go back to visual studio and we need to update the plant object and also the plot manager in order to add this plant and harvest transaction so over here on the plant object we can say instead of price we're going to be saying buy price okay and we're gonna be needing also a new public ant that is going to be our sale price we can save this and here on the plant item we no longer have a price so it's a buy price save this as well and heading back to plot manager whenever we plant we need to make the transaction but first we need to know if we have the money in order to do so here we're going to be checking that the farm manager is planting and the farmmanager.selectedplant.plant.com buy price okay it has to be lower or equal to the current money you have so fm dot money if it's lower then you can go ahead and plant if it's not lower then you're not going to be able to plant and over here we just need to make a transaction whenever we plant we need to tell fm dot transaction and here it's going to be a negative transaction because we're going to be subtracting it from the money so selected plant dot by price okay and whenever we harvest we're going to be doing the same but this time here we're going to be getting money so it's not going to be negative it's going to be positive and instead of the buy price we'll need to reference the sale price here we are selling here we are buying okay it's very simple we can save this and when we go to unity we just need to fix here on the corn we're going to say the buy price is 10 the sale price is 20. here on the sunflower the buy price is 20 the sale price is going to be 50. and here on the tomato the buy price is 30 the sale price it's going to be 80. so when we hit play the money should be working let's check this out whenever we select the corn we can buy it and it's subtracting here on the money whenever we select the sunflower we can plant with it and now we don't have enough money to buy the tomatoes so when we click buy we should not be able to plant and we're not able to plant so it is correct so here we can deselect and when we click to harvest this plant it seems to be working we're getting the money here okay another thing that we can take notice off is for instance right now we have ripe tomatoes over here and we have 10 bucks so we can buy corn okay and the corn is selected you can see it down here and when we click we harvest the tomato that is not the expected behavior the expected behavior is supposed to deny us harvesting the plant because it's currently trying to plant a new plant and it's not going to be possible to plant somewhere that is already taken so the expected behavior is for nothing to happen and currently when we have some plants selected we can still harvest and in order to fix it we can go over here to the visual studio and here on mouse down we're gonna be needing to check over here if fm is planting so we're gonna say that it needs to be on the last stage and fm must not be planting okay so not planting its planting must be false so when we save it this should work let's head to unity to check to see if it's working so here i can hit play and when we buy it we can check to see that it's working whenever we buy it and now we have corn selected over here and when we click to harvest let's see how it does it doesn't do anything okay it's working properly and when we stop by it so we have deselected it it goes back to harvesting so this is working properly we can pause this for now and what we need to do now is first we need to verify what we have selected so a visual way of showing that we have something selected and whenever we are hovering over plots we need also to know what plot we're hovering over so what we'll be working now is visual cues for that the first one is going to be whenever we select to buy a plant it's going to be showing cancel here so we can cancel the selected plant that is the first thing we're going to be working on and to do so what we need here is earth here on the scripts we're going to be needing on the plant item a reference to this button both to the button itself and the text on the button and here on the farm manager we're gonna be needing to update this button so here we can go to the plant item and ask for a public image that is going to be the button image and we're also going to be asking for a public text which is going to be the button text okay we can save this and at the beginning it's going to start how it is on the scene and here on the farm manager we just need to update it when we select or deselect a plant so first we're going to be needing references to the colors we're going to be using so i'm going to ask for a public color called by color and it's going to be green and here i'm going to be asking for a public color cancel color and it's going to be red okay we're also going to update it later on the inspector so here we're just deselecting a plant that we've just selected so all we need to do is we need to take the select plant okay we're gonna be accessing its button image dot color and we're gonna be using the bi-color because if the plant is selected it currently shows a cancel button and we're going to be updating that okay so we're making it go back to buy and here we can copy this and right after we select the new plant we can paste it and instead of by color it's going to be the cancel color and instead of by it's going to spell out cancel just one more thing whenever we select a new plant there's a chance that it's overriding a plant that we've selected before when that happens we need to reset the button on the selected plant to go back to buy okay so if select plant is not new this means that there is a plant already selected before we change it to the new plant we need to update its button to go back to a buy button so we just need to paste what we copied here and that should be working we can save this let's go to unity see if it's working and here on the farm manager we can change the buy and cancel color i'm gonna go with a let's see let's see here on the plant item we have the button so this color and i'm gonna copy it and paste it on the farm manager by color and the cancel color i'm just gonna choose a less bright red you know here looks good and also as we saw here on the plant item it's missing its button image and its button text we can fix it by going over to the plant item and dragging the button and the button text here then we head over back to the scene and hit play just to check this out we can buy and here it spells out cancel we can cancel it and we've de-selected it now we are not buying anymore and we can hit buy here buy here buy here and as we can see it's working properly this is great so now all we need to do is whenever we hover over a plot it's going to show us if we can blend there or not okay and to do so we're gonna be using the plot manager script okay we're gonna go to visual studio in order to use it we can now delete this because now we know when we've selected a plant so i'll be deleting this we're going to save this and over here on the plot manager just like we have a on mouse down function we're going to be adding a on mouse over function and whenever it's over the plot of land it's going to be checking if farm manager dot is planting if it is it's going to be checking if you can plant there or not so the conditions for planting is first you can't plant if it's already planted so if it's planted or if your money is shorter than the buy price for the plan that is selected so here we're just going to copy this and instead of checking to see if the buy price is less or equal to the money you currently have you're going to be checking to see if it's greater because if it's greater than here you can't buy and we're going to be making a visual for that else you can buy okay it's very simple so to show the visual here we're going to go with two colors just like the buy button we're going to go with a available color and a unavailable color so here we're going to be asking for a public color that is going to be the available color and it's going to be green and in order to use these two colors we're going to be needing a reference to the sprite render of the plot okay so we need a sprite renderer that is going to be the plot and here we can say that the plot equals to get component sprite renderer okay this is looking great and here we just need to set the plot dot color as the unavailable color because here you can't buy on this plot so the color here is going to be unavailable and right here we can do the same but with the available color and whenever we exit this we can call the on mouse exit and change the plot color to color dot white this is going to make it go back to normal when we save this we can check it out on unity to see if it's working so here there's nothing going on when we select to buy you can see it goes green when we buy here it no longer is available for us to buy so it's red and here it's still green because we can buy here now that we have 20 here we can check to see if we can buy the tomato and as we can see it's red telling us we can't buy okay this is looking good this is great and the visual cues are all here so that was it for today guys i hope you enjoyed it if you liked it don't forget to leave a like if you have any questions or you want to say something please comment down below and if you want to see more videos like this or stick around for part 4 please consider subscribing so that's it for today thanks for watching bye
Info
Channel: SilverlyBee
Views: 1,545
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: K0hsBFairko
Channel Id: undefined
Length: 50min 43sec (3043 seconds)
Published: Fri May 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.