 Today, you're going to learn how to create your very first click upgrade right here. Oh, look how rich we are. Hello, everyone. It is Crypto Grounds here. Welcome back to another Unity idle game tutorial video. This is episode two, and this is part of the 2021 series. And today we're going to be setting up our upgrades. And today we're specifically going to work on the click upgrades for our game. So anyways, if you enjoyed this video, make sure you smash the like button, subscribe to my channel if you're new, and turn on the bell for future notifications and live streams. If you want to support me, click that join button below or check out the Patreon and description below. Anyways, let's just get on with it. So in the previous episode, 1.8, we were able to set up GitHub desktop right here. So if you follow it along and you remember me changing the project name, and if you were able to change your folder name as well, you may notice that if you go back to Unity Hub and your project is gone. If that's the case, just simply click on this add button and scroll down to your project, which mine is right here out of game tutorial series 2021 repo, and select folder and it should put it back right here. And we'll just open it right up. All right, guys, we are in Unity. And so the first thing we're going to do is create a brand new script or class. And we're just going to go to our scripts folder, right click on it and go to create C sharp script. And we're just going to call this the upgrades manager. So this is where all of our upgrades are going to go. And we're just going to keep things organized. So now another thing we're going to have to create is, and we're also going to have to create another script. And we're going to call this upgrades. So this will be used for our buttons and generally just making it easier for us to manage individual upgrades, like being able to change all the text, the colors and stuff like that. So currently we have the upgrades manager open, but we also want to open data and upgrades. Alright, so in our data class, this is where we're going to store everything. Obviously, we're going to create our first upgrade in here. And we're just going to need to store the level only. So in the past, in the previous series, what I did wrong is that I stored everything in here, we're not going to do that. We're going to do things much differently in here. And it's going to be amazing. So we're going to create a big double object here. And we're just going to call this the click upgrade. Okay, so we're going to add more eventually. But we're going to do it in a completely different way. And I want you guys to know that we will be adding more in the future just differently, not just copying pasting. Okay, click upgrade, click every two, click up a three. Okay, we're going to do things completely different. So make sure you just keep going in the series and you'll know what I'm talking about. And for now, we're actually going to call this level. But we're going to set this click upgrade level equal to zero in our data constructor here. Okay, so cool, we have our click upgrade level in here. Now how we're going to determine the cost and all that stuff. So let's create a few variables here. So we're going to create one in here called the base costs. So we're just going to call this click upgrade base cost. And the next one we're going to add is the cost multiplier, because we need to somehow calculate the growth of the cost. So in here, we're just going to call this click upgrade cost molt. Well, we need to initialize these because I mean, we can just set this equal to 10 in here and then just set this to 1.5. But once you set these variables to a number, they won't change until you recompile your game, which is like building it. So we're not going to be initializing stuff in here, we're going to be doing that inside the public void start method. So we're just going to set our base cost equal to 10, for example. And then we're going to set our cost molt equal to 1.5. Okay, so basically, the cost is going to multiply by 1.5 every time we buy an upgrade. Okay, so now where do we bring this all together into a magical little soup? Well, we have our level, we have our base cost and our cost molt. Where do we do the black magic? Well, all we got to do is simply create a big double cost method. Cool. So now we're going to do everything in here. And we're just simply going to return the cost from in here. Now we will be modifying this throughout the series. So be sure to keep an eye on that. But for now, all we need to do is simply just do return base costs, click upgrade base costs. And we're going to multiply this by big double dot pow. And we're just going to do cost molt. So I actually just realized I spelled that wrong. My bad. You didn't see that. But yeah, we're going to basically just do base costs times our cost molt to the power of, okay, where's our level? How are we going to access this now? We can just do. Okay, so remember early on in the series, we created this controller dot CS class here. Well, it has our data in here. So this is our monster. This is our central powerhouse. This is where everything's just going to be focused on. This is where everything's going to be controlled from. Okay. So what we're going to do in our upgrades manager is create a brand new data object here. Now the thing is that if you modify this one right now, it's going to change a separate one inside upgrades manager, not the one that we created in controller. Okay, they're two separate objects. They're just the exact same name. So now how C sharp works is that if you were to set an object to another object, you're basically setting the reference of that object to the original one. So here, I'll show you how we're going to do this. But we're also going to add in here, which we're going to change in the future. But for now, just to keep things basic, we're going to add our controller object in here. So we're just going to call this controller lowercase like that is what we're going to do is set data equal to controller.data. So now we're basically setting this data to a reference of controllers data in here. Okay. Now an alternative method to this is simply just ignoring all this and you can literally just do controller.data and then you can access the level here. Okay, it's the same exact thing. So I'm going to keep mine like this for now, just to keep things simple. So don't forget that semi colon and it is good to go. And we can convert this into something called an expression body, where if you have something that's a single line like this, you could basically just copy this line here and create this Lambda arrow right here and paste it in here. So basically, this only works for one line one liners like this. So basically we're just returning whatever this is. Okay. And that's the same thing as just sweet. So now if we were to return this cost here, we would see that the cost would be just 10 because we have zero levels. But if the level was one, it would be 15. And so on, because it multiplies by 1.5. Wow. So what do we do next? Okay. So instead of just jumping straight to the buying and all that stuff, we need to actually create the UI real quick. So I'm going to go ahead and create a button real quick. Okay. So here's our click upgrade right here. So basically it's just an image. It's the same thing as this. And it has a button component. We need to change this. I'm just going to get rid of that quick. And it has a little level section here. And I'm going to anchor this to the left side right here. And inside has a text, which is stretched. And then we also have a name and a cost. And we're actually going to make these anchor to the right side. And our click upgrade is going to anchor to the left for now. We're going to eventually move this once we have more upgrades. But yeah, that's it. So next, what we're going to do is actually assign this upgrades right here, this upgrade script that we created, we're going to assign it to our click upgrade game object right here, just right to the bottom. So now let's open this up. So now we're actually not going to really do anything in here other than just create our objects that we already have. So let's see what do we have here? We have a game object that represents this whole thing right here. And we have a level text, a name text and a cost text. Now you can manage this color if you want to, however, I'm not going to be really be doing that. So I'm just going to be making this an image, a text, a text and a text. Okay, so now we know what to add. Let's add an image. And this will be our actual game objects upgrade one. That sounds good to me. And we also need to import using unity engine.ui. And we also need to import the TM pro. And we get to leave these top two lines for now. And we're just going to add a TMP text up here. And this will be our level text. Remember, we have a level text, a name text and a cost text. Okay, so this is all we're going to do in here. Seriously, this is all we're going to do in here. We're basically just going to create an upgrade object in here. Okay, so now in order to actually edit this, what we're going to do is go to our upgrades manager. And in here, we're going to create a public upgrades object in here. And we're just going to simply call this the click upgrade. So now in here, in our public void update method, we can start changing things around. For now, we're not really going to do anything with our upgrade button here, we can eventually make it turn green or red if we can afford it or not, stuff like that. And but we're just gonna leave as is for now. And we can add the level text, we're going to add the name and the cost. So let's start with the level here. So in order to change things inside of this upgrade, it's the same thing as changing a variable or accessing a variable in, for example, the data class, all we got to do is do click upgrade dot level text. And we're just going to set this to our controller dot data dot click upgrade level. So we're not done here. So you see, I forgot one important thing, okay, we forgot to add the text attribute here. So in order to change the text, we need to do dot text. So don't forget about that. A lot of people have forgotten that in the past. So just remember, if you want to change the text, you must change the text field. That's part of the text. Okay, the text variable here, just don't forget about that. So now the other thing is that you cannot convert a big double to a string. Okay, now this is the same thing with ins floats doubles. And this people, a lot of people have this in the past. So what we can do here for now is do dot to string. So basically, this just converts the big double to string. And that will make compatible for what we would need to do here. So next let's do our cost upgrade. So we're just going to do a click upgrade and then our cost text dot text. And we're going to set this to whatever name we want. So let's actually create a string here, because this will be very important the future. So this will just be a very separate string. And this is going to call the click upgrade name. And in the start method, we are going to assign this a name. And for now, I just did plus one flask per click. So what I'm actually going to do is just do flask per click. And that's good. So now for the cost text, we're going to simply add a string called cost. And we're going to add a space. We're going to add it. So we do it by adding the plus. And we're just going to do our cost right here and then flash. Okay, so now in here, we just simply call the cost method because remember it returns a big double. So we also need to do plus flask. Okay, so now since we don't need to do dot two string here, why? Well, it's because we're adding a string to a normal variable already. So it's going to convert it to string and it'll be okay. So now for the name text here or our description text, I think I call this name. I don't exactly remember. Yeah, name text. So our name text here, we're simply just going to do plus one space, and then just flask per click. So we're just going to add our click upgrade name here. So we're going to eventually modify this once we add a future prestige system, other upgrades, all that good stuff. So now every frame, it's going to be changing all of this info. This can be optimized. Okay, this can be optimized. We will get there. So now we need to actually buy the upgrade. So this will be very easy. So we're going to have to do is add a brand new method here. And I'm just going to add it's below this cost here. And we're just going to call this public void buy upgrade. Okay, and we're simply just buying a single upgrade, we're gonna have to modify this as well, just like the cost. So make sure you just keep an eye on that. So how do you buy an upgrade? Well, first, we have to check if we can afford that. So how do we do that? We're going to create an if statements. And we're going to check to see if our flash is greater than or equal to the cost. So we just do controller dot data dot flash. If it's greater than or equal to cost. Okay. And that that's true. In here, we're going to subtract the cost first. Okay, we don't want to change the level and then the cost because if we change the level, we are increasing the cost. So make sure you do that first. That's the very first thing you need to do in here. So flash minus equals cost. Perfect. And now we can simply add the level. Oh, yeah, I gotta add the level. So plus equals one. So what you can do is plus plus, or plus equals one. But I'm just going to do plus equals one because who knows, we may have some random multiplier that could affect this later on. So just keep in mind. So earlier, I was talking about optimization. So I don't really want to update this text every frame because that's just gonna, that's just pointless run code that we're running here. And that could really add up in the future if you don't optimize this. So what we're going to do here is create a brand new method public void. And we're just going to call this update UI or update click upgrade UI or something like that. So basically in here, we're just going to bring all of our UI stuff that we've changed here, like all the text stuff. And we're just going to paste it in here and get rid of this old one here. So updates pretty much useless right now. So we can just get rid of that. So now when do we update this click upgrade UI? Well, we want to load it after we initialize with start. And we also want to do it after we buy an upgrade. So I'm just going to put it at the very end here. So now this will only be run every time you load the game. And when you buy an upgrade, so you're basically calling it much, much less. And that's very good practice. So there's one last thing we need to do. And that's this start thing. So to prevent some future errors here, or not errors, but some saving and loading issues, because we will have a saving load system. I want to be able to start this after we load our data. So for now, we're just going to call this start upgrade manager. And we're going to call this inside our controller script here. So in our start method here, we're simply just going to do, first of all, we need to add our upgrades manager to the controller class. So we're just going to do upgrades. And now in the start, we're going to call upgrades manager dot start upgrade manager. And now this will happen after we start the new data process and all that. So now the reason why we want to do this is because we want to update the UI, we want to have all this stuff be done after we create this data class or else you never know. It may display the wrong variables and stuff like that. So that's just a safety precaution. Sweet. So now we have everything set up. But I lied, there's one more thing. We have to actually apply this level to our click here, because right now it's still adding by one. Okay. So what we're how we're going to do this. So and we're just going to create a method here just to prevent creating a mess later on. So what we're going to do is make a method called click power. And this will be only for our click button here. Now we can add it in here, but it will be pretty lengthy and I'd rather just calculate it in case we want to, you know, use it for something else, just for display. So in here, we're simply just going to return one plus data dot or upgrade level because this upgrade level is only adding by one. And we already have one. So we're just going to do that. And again, we can make this into an expression body like that. It looks beautiful. And in here replace this plus equals one in the generate flash method with click power just like that. And you can call this whatever you want. You can call this flash power, coin power, whatever your currency is, you can name it whatever you want. So I'm just going to keep a click power just so you know what it does. And I want to be able to see what my total click button here is. So we're going to add another text. And this will be a serialized field. This would be a private TMP text. And in the update method, I'm just going to add this in in here. It's just plus one space flash. So we're just going to add that in here. So we're going to have our string right here, which is going to be plus. And we're going to add that to the click power. And then we're going to add that to space flash. Cool. So this is what our scripts look like right now. Beautiful, beautiful. If you have any questions, make sure you just leave your comments below and I will be sure to answer them. And I think we're all set. So let's hop back to unity. Okay, so we're going to go to our scripts here, go to our controller here. And now we need to upgrades manager. We actually haven't had that yet. So let's go to our scripts here and create an empty. And we're going to call this the upgrades manager. And we're going to drag our upgrades manager script into here. And there you go. So let's go back to controller here. So we're going to drag our upgrades manager into this box here. And then our flash click power text that will be in the increment button right here. I'm actually going to just call this the click power text just so we can be a little consistent here. And our upgrades manager, we need to drag the controller and our click upgrade. So remember, we assign that to this click upgrade here. There's the upgrades right here. So let's drag that in here. And we're going to go to our click upgrade here and drag in our upgrade, which is the image here, our level text, our name text and our cost text. Okay, so now we need to add to this on click function right here or this method here. So we're going to click the plus drag the upgrades manager, no function upgrades manager and buy upgrade. And that should be it. Honestly, it's very simple. Okay, let's zoom out. Let's test things out. Let's see how they are. Oh, I forgot. I forgot to remove this old code. My apologies. So I simply forgot to remove this data flash multiply right here. So just get rid of that. I'm sorry. Okay, so we start with one flash. So I think that's just what our data is. Oh, yeah. Okay. So we started because of here. So we started with one flash because of this right here, this data flash equals one. So you can just get rid of that. I'm not paying, I wasn't paying attention to that. But anyways, back in here, so we can click, we get one. And now we can afford this right here. So if we buy it right here, increase the cost increase our level and increase how much we can get per click. Nice. It's very simple. It works. And eventually, we're going to get into the mess of seeing a lot of decimals here. So as a temporary fix, what we can do is go back to here, our cost and upgrades manager here and do dot to string. And then in here, we're going to format it to display only two decimals. So we can do F2. So now it's going to show two decimals and F is for fixed point. There is n, which is where you can get the comma formats, but there's some rules to that. So you'll get to that later on. Please do not use this. So we have F and and P for percentage, which is kind of cool. I don't think it would work for big doubles anyways. So all I know is that f works and we will fix that later on when we create our notations. So anyways, crew, if you guys enjoyed this video, and if you learned something new and it was very helpful, make sure you smash like button, tell your friends about it, helps grow the channel, and maybe you can help someone out too. Subscribe to my channel if you're new and if you enjoy this content and turn on the bell for future notifications of videos and live streams. I want to support my channel and want to help support my game development and videos. Click that join button below. Every single contribution helps a lot. And I want to personally shout out to everyone who is currently supporting me right now. It truly means a lot to me guys and I wouldn't be here without you guys. And also there's a Patreon link in the description below if you're interested as well. Yes, we got some awesome merch in the description below. Make sure you check that out. So anyways guys, I will see you guys in episode 2.1. I'm going to be showing you guys how to create single tins. Trust me, they are very simple and they are a lifesaver. They are truly a lifesaver. So I will show you guys how to properly use those and I will explain what those are as well. So make sure to tune in for that. Anyways, I'll catch you guys in the next one. Thank you guys for watching. Have a great day and night. Peace.