 In this episode, I'm going to be showing you guys how to add production upgrades to your idle game. Hey guys, what's up? It is Crypto Grounds here. Welcome back to another video. This is episode three to the Unity Idle Game Tutorial Series 2021 edition. And as I said before, we're going to be doing production upgrades. If you enjoyed this video and if this video was helpful, make sure you leave a like and subscribe to my channel for new and turn on the bell future notifications of videos and live streams. Let's just hop right into it. So what I've done is clean up the interface a little bit. I've added some texts and I added a brand new panel for the production upgrades. It's the exact same thing as click upgrades. I just renamed everything in here so we can keep everything organized. So I don't need to show you how to do that, but we're just going to jump straight to the code because that's where everything starts. So first we're going to start in our data script. So we need to add our variables here. So we're going to do the same thing as our click upgrade. We're going to create a brand new int list. And we're going to call this our production upgrade levels. And we're going to create a brand new int array and just convert that to a list. So next we need to create an upgrade list. And this will be for our production upgrades. And we will also have the same upgrades prefab, except we're going to create a brand new one that is special for our production upgrades. And I'll do that after this. And next we need to create a brand new scroll rect for our production upgrades. And I'm going to make a brand new transform. Okay, I've created the scroll and the panel. Next we need to create a new string array for our names of the production upgrades. And we will do the exact same thing with our cost information or upgrade information. In our start method, we need to assign our information. So we're going to create a new string array. And I'm just going to come up with four random names for these. Okay, so our first upgrade will be one flash per second. And then we're going to have two 10 and 100. For our base costs, we're just going to do 25 100 1000. And maybe something like 25,000. I'll just do 10,000 for testing purposes. And for our cost multiplayer, I'm going to do something a little higher than our click upgrade costs. And that will be 1.5, 1.75, two, and maybe three. And also I need to rename these. And for our base power, it's just going to be one, two, 10, and 100. To instantiate our upgrades, we're going to do the exact same thing as we do above for the click upgrades, except we are using our production upgrade levels inside of our for loop because we may have more production upgrades than we do for click upgrades. So that's why we do these separately. And to instantiate, we are going to use our production upgrade prefab. So we are creating a brand new object based on our production upgrade prefab. And we are going to throw that inside of our production upgrade panel. And we're going to set its upgrade ID to our I variable inside of this for loop. And we're going to add this to our production upgrades list. And when we add two upgrades, we want to reset the position of our scroll. So we're going to grab our production upgrade scroll, set the normalized position to a brand new vector to and set the x and y to zero. So for updating our upgrade UI, I could just copy and paste this. But that's a bit repetitive. So we're going to be managing this. We're going to be changing this update click upgrade method to just update upgrade UI. And now we're going to merge both of these together. So what I'm going to do is I'm going to add a string parameter in here, and we're just going to put our upgrade type in it. And in here, we can just insert, I don't know, click just for an example here. And also we need to rename this. And now we can manage this type here. So now we're going to actually replace these click upgrades with a parameter. So we're going to create a list of upgrades parameter. And we're just going to call this upgrades. And we can replace these with upgrades. And again, like I said in the previous video, we don't need to add this ref term here. So with our string type, we're going to put a switch here. And you never know, you may have other types of upgrades. We could just do an if an elf statement, but we could always add on in the future. So I'm just going to use a switch just so we don't have millions of if statements. And so you guys don't get in the habit of doing that. So for our click, we are going to copy and paste this and add make sure to add a break at the end. We're going to do this exact same thing as we did before, except with our update UI, we need to add our click upgrades as our first arguments. And this goes for the else right here. And now for production, we can just copy and paste this replace click with production and replace our click upgrades with production upgrades. So now again, this would be a little bit of a copy paste. We can simplify this if we want to. So I'm going to leave it as this because generally the rule of thumb is that if you do the same thing three or more times, that's when you want to convert it to a method like this. But we only are only doing this twice. So we don't need to if you want to give yourself a challenge, go ahead, go ahead and try to do this, try to convert this into a method. So you only just call one single line per type of upgrade. So now when we update our upgrade UI, we want to do the same thing update upgrade UI, and we just replace this click with production. So we still have an error here. And that's because we need to rename this method as well. We still need to add our string arguments that says click. Alright, so now we have all the UI. So for the cost, we're going to be adjusting this method, and we're going to merge the click in production upgrades. So this is going to be called upgrade cost. And we're going to get rid of this lambda here and convert it to a statement body. So it looks just like this. And now we're going to use a switch statements because we need to add in a new string parameter for our upgrade type in our switch, which is based on type. And for our click upgrade, we're just going to copy all of this pasted in here. Now this is a bit lengthy. I know this is really lengthy. So I'm going to create a data variable. We're going to assign that to controller instance data. Get rid of that. It's a little shorter, not too much, but we'll do. And for our production, we're doing the exact same thing here, except replace our click upgrade stuff with production. Otherwise, if we don't have these for whatever reason, we're just going to return zero, we have some red. So the first one is inside the update UI. So as you may notice, we've been grabbing the click upgrade cost entire time. So we would be getting the wrong cost, we can just replace this with the upgrade cost and we can add our type as our first argument. And for a by upgrade, we're going to be doing the exact same thing in here, we need to add in the type. However, we don't have that yet. So let's add in a new parameter, same thing. And then we can add in our argument. So we still have our click upgrade level. So I'm going to create a new method here. So we can determine which upgrade we're actually upgrading. So we're going to be creating a new method called by and all we need to put in is an int list. So now this is very important. If your lists have different types, if you have an int and you have a long or I don't know a big double, for example, this is not going to work because you are going to put in an int list, whatever both of your upgrades are, you're going to pass in an int list, not a long or big double list, but an int list. So if both of these are big doubles, then you can pass in a big double list. However, originally, we have our int list here. So we're going to be passing an int list into our by method. I hope that makes sense. But just know that these much match this one right here. And we're going to copy and paste all of these in here and replace our click upgrade level with upgrade levels like that. And our update upgrade UI is actually going to be our string type. So now we need to determine which one we're actually calling. So we're going to create a brand new switch statements, replace it with type and the same case click and production. So case click, and we're going to be calling our by method and putting in our data dot click upgrade levels. And we're going to break it. So for our production, copy and paste it, same thing, we're going to do in production and replace our click upgrade levels with production upgrade level. That is all we need to do here. So in order to actually call this, we have to go to our prefab script right here. And we have a red here. So we have to buy upgrade. And we still have to add in our type. So this is a click upgrade. So we're going to do click. We're adding a click argument string, a string argument. And we are going to copy and paste method and replace this with by production upgrade. And we just replace a string with production. So now this will buy the production upgrade and this one will buy the click upgrade. Let's head to unity. So the first thing we need to do is create a brand new prefab. And really, we can just duplicate this. So what I'm going to do is I'm going to drag this click upgrade prefab somewhere around this canvas doesn't really matter where and I'm going to right click on this and go to prefab and unpack completely. I'm going to rename this to a production upgrade. And I'm going to change this button method that we're going to call on click with buy production upgrade. And nothing else that we need to change here. Now we can just drag this into our prefabs folder and create a brand new prefab and we can delete that. So in our scripts and our upgrades manager, we just need to add our production upgrades prefab. So our production upgrade, we need to add our scroll and our panel, which I have already created. And that is all we need to do. So now we can just give it a shot. Cool. So it generates all the upgrades. So now our upgrade titles are a bit mixed. So we still see click upgrade power one, but we see the correct costs. So if we were to buy this, let me get there first. You could see that it's not going to do anything. It's because we haven't actually done the production. So we're going to have to do that. We're going to have to create our per second text. So let's do that. First, I want to fix the title. So in our UI, we can see that we are accessing the titles somewhere. So our update UI, ah, here it is. So our click upgrade names. And we're still accessing our click upgrade level. So we are grabbing the wrong data. So we're gonna have to pass in a few more parameters. So our first one will be, let's see, what is this? An endless list? Yes. So what we're going to do is pass through list int. This will be our second parameter that we're going to add. And we're just going to call this upgrade levels. And the second one is going to be a string array, because that's what our click upgrade names are. So a string array, and this will be upgrade names. I'm going to replace the original list and the original array. And now we need to pass them through as an argument. So for a second argument, we're going to be doing data dot click upgrade levels. And for our array, it will be click upgrade names. Let me close this. There we go. You can see all of it now. And we're going to do the exact same thing with this one down here. And for our production, we're going to be doing data dot production upgrade levels and our production names. And I'm going to copy and paste these into the second call. So now we should see the correct names and the correct levels. So we need to do the production. So let's head to our controller script. So we have our click power, we're going to create a similar method for our production. So this will be called flask per second. And we're going to be calculating this total exactly the same way as we do for click power. So we're just going to replace a few things. And our total is going to be zero. And in our for loop, we're going to be looping through all of our production upgrades. And we're going to multiply the total by our base power. And this will be the production upgrade base power and our production upgrade levels. And then we're going to return the total. That is pretty much it. That's it's literally the exact same thing here. And once we add boost, we will change this and we're gonna have to tweak it. So just a heads up. Now for our flask for seconds in order to actually generate the flask inside of our update method, we're just going to call data. We're just going to add on to our flask. So plus equals, it's basically where we plus equals, I think I've said this in the past, but it's basically data dot flask plus whatever we want to it. So it's basically we're setting it to its current value and we're adding on to it. So and again, this is the exact same thing as plus equals. So we're missing one more thing because this is going to add our flask per second every frame. So if our game is running at 60 FPS, we're going to be getting 60 times more flask per second than we should. So in order to fix that, we need to multiply it by time dot delta time and time dot delta time. It's basically the difference in time from the last frame and the current frame. And the description here, it says the completion time in seconds since the last frame. So now with this, we will successfully generate whatever we want in here per seconds. So yeah, that is all we need to do to generate flask per second. So let's add a text. So we're going to add a new serialized field. And this will be a private TMP text. And we'll just call this flask per second text. And inside our update method, we're just going to set this equal to dot text. And I'm going to show you guys something. So this is called string interpolation. So basically, it's where you can add your variables inside of strings. So you add the dollar sign, and then the parentheses, and you add a curly brace inside of the string, and you put your variable or method or whatever in here. So in our case, it will be flask per second. And in here, you can just do I don't know, like, slash s, and then I could just, I can do colon f two. So colon f two, this is pretty much the same thing as doing string example flask per second dot two string f two. So this is the exact same thing. This is how we format stuff inside of string interpolation. So we can even convert this. And I don't know if your ID has it, but mine does, you can just do all to enter, and you can just convert it like this. So it converts everything. So yeah, you can either have it like, pluses like this, or you can use string interpolation and make it look like this, it makes it look clean. All right, cool. So now we should start getting some production. All right, so now in our controller, we need to add our flask per second text, which I have not created yet. Let me do that real quick. Okay, so I have created the per second text, and I'm just going to drag that into our box. Sweet. Our UI looks a bit jinky. So we can adjust that. Yeah, the text is a bit too big. Let's buy this upgrade and see if it works. All right. Oh, no. Okay, cool. It works. But we have to apply the formatting to our flask. But yeah, at least we know it works. So let's do that while we gain flask for quick. So that will be the same thing data dot flask. And we can just do colon F2. And there we go. Now we should see two decimals. Once we refresh it. But let's just keep going. Let's just see how far we can get. Let's see if the two flas per second actually works. I think that might be a little too expensive, but it's okay. It's your guys job to balance the game. There you go. As you can see, we're getting seven flas per second. And yeah, let's refresh it real quick. All right. So there you go. Now we have two decimals here and it looks clean. So now we shouldn't see this mess. There you go. That is our production upgrades for you guys. So one more thing I'd like to mention is that with your cost here, you need to cast your levels as a big double inside the break infinity's pow, it doesn't automatically cast the int that we pass in as a big double. And this can cause some issues when we get higher numbers. So make sure you cast these both as a big double. If you have any questions, make sure you leave them down below. Hopefully this video was pretty simple to do compared to the click upgrades. And so that was all the messy setup. This was pretty much just expanding onto it and making sure that we can add whatever we want to in the future. If you enjoyed this video and you found it helpful, make sure you leave a like and consider subscribing if you are new around here. Be sure to check out some of my other tutorials if you're looking for some more stuff like this. And if you want to get notified for future videos and live stream, make sure you turn on that bell. If you want to support the channel, click that join button or the Patreon and the link in description below. Also come join the discord. It is also in the description below if you want to be part of the community. Thanks for watching again. And I hope you guys have a great day night. I'll see you in the next one, which will be episode 3.1. And that will be making an organizer. So I'm basically going to merge these together. You'll be able to switch between click and production upgrades just to keep things organized. Anyways, see you guys in the next one. Peace.