 All right. Welcome back. So we're going to keep going with our object replace tool or replace objects tool. And what I want to do is start to get some more functionality in place. So currently we have an empty window with a very generic label. Okay, so what I need to do is I need to add what they call an object field. So we can assign objects that we want to have be put in place of whatever exists in the scene. So what are we going to replace it with? Okay. So let's go back into Visual Studio over here. And the first thing I really want to do is I want to keep track of how many objects I have selected. So up in the variables region up here, I'm going to declare a private integer. And I'm going to call it the current selection count. All right. And we'll just initialize it to zero. That's fine. And what I want to do is I want to put this into a separate function. Okay, so let's give ourselves some notes here. So I'm going to check the amount of selected objects. All right. And we're going to we're going to actually put that functionality into a method. So we're going to create a private method called get selection. Yep. There we go. That'll be fine for now. And we can actually do a lot with this. But for now, what we're going to do is we are going to just keep track of how many objects we have selected. Okay, so this will be one of the first steps in managing whether or not there is something actually selected. Okay, so what I want to do first is every single time that we go and check this, I'm just going to set the current selection count to zero. This will be zeroed out. And then just reassign it with the selection dot game objects. All right. Now you notice that the selected game objects actually returns an array of game objects. And in this case, this isn't exactly what we want to do. Okay. I actually just want to get the length of that for now. So that'll give me the number of selected game objects in the scene. Okay, so with that in place, let's just call this now. Okay, so let's put it up here and we'll call it cool like so. And what I want to do is start to add a little bit of styling in here. So what we're going to do is we're going to display the current. All right, so we're going to say the selection count. Let's do something like that. Okay. And we'll just add on that current selection count dot to string. All right, so we'll convert it to a string. Cool. But what I want to do is I want to put all this into a bit of a layout. So we have control over the spacing and stuff like that between the different UI elements. So in order to do that we are going to put down the editor GUI layout dot begin vertical method. This allows me to create a vertical layout right off the bat. And anytime that you open up a layout, you need to end it. So there's a beginning and an end. All right. So there's a vertical layout. So to visualize that a little bit better. It's laying things out in a vertical fashion inside of the window. Okay, so now we have our label updated right here. And we are now inside of a layout. And what I want to do now is I actually want to give it a little bit of space. I'm going to say editor GUI layout dot space, like so. And we'll do the same for the bottom just so we kind of pad it a little bit at the top of the bottom. Cool. All right, we need to put a semicolon right there. Keep in mind that we put the repaint down here. That way it's not inside of the layout. We don't want to update while we are inside of a layout before it gets closed, basically. Cool. So now if I were to go into the Unity scene here, just keep our tool open, like so. What I can do is I can come in here and I am going to create a couple of, let's just do spheres for now. So let's create a bunch of spheres. I'm going to just control D and copy and paste a bunch of these guys around here. Okay. And now what I want to have happen is I need this particular tool right here to show the count. In fact, we are already getting the count. So you can see that it's updating in real time because we have that repaint in place. All right, so we're getting the count now. Cool. So with that, we have that first part. This will help us check to see if we can even replace stuff because if we don't have anything selected, then obviously we're not going to be able to replace anything. So that's our first check that we're going to do. But before we get into that, what I want to do is drop down another GUI layout space up here and just put that right there. And then right below here, I'm going to do the object field. And in order to do this, I need to create another private variable up here that is a type game object. And this is going to be our wanted object. So in this video series, we'll just have it support a single object. All right, I'll probably extend it later on to support multiple objects and randomization and all that cool stuff that makes these tools really useful for level designers. All right, so now that we have that private variable up here, what I want to do is I want to place that down there. And it's going to be equal to the result of whatever this GUI layout dot object field has inside of it. So let's take a look at the arguments for this. So you can see it has 10 overrides. It's quite a few. So the one that we actually want, all right, let's go down here and see, there we go. There we go. That's the one that I want. The very last one. All right. So I'm going to provide it a string, a label, and then we need to provide it the object. All right, and that's going to be this wanted object. We need to provide it a type. That's going to be game object. And then we want to say that we do want to allow seen objects in order for this to work. So you can lock that off and say it only supports objects from the project, but we do want to allow any object to be assigned. So I'm going to set that to true. Okay, so first thing that we want to do is call this the replace object like so. And then we want to use that wanted object variable that we created and we want to set the type. So type of game object like so. And then true because we want to allow seen objects like so. But you'll notice that it is actually still throwing an error for us. And that's just because it can't implicitly convert this object type to game object. So we need to do one more check even though we're telling it the type of object right here. We still do need to do the cast. So let's do game object like so. And that takes care of that. So if we were to flip back over here to unity, you'll see that now when it finishes compiling will get an object field. So now we can drag and drop stuff into this. All right, so you can take a sphere and put it in there. All right, or you can take something out of the project hierarchy and put it in there. So really flexible way to allow artists and designers to utilize stuff in the hierarchy or the project. All right, so the last UI element that we need is our button. So I'm just going to put that right below the object field. And we're going to say if our GUI layout dot button. All right, so we're going to create a button and it's inside a GUI layout, not editor GUI layout. And what I'm going to do is just call this replace selected objects. And I'm going to set a couple of these options. You notice that most of these UI elements have these GUI layout options. So in order to use these, we just say GUI layout dot expand width. So it's one of the options. I'm going to set it to true. So I wanted to stretch all the way across the layout. And we're going to say GUI layout dot height. All right, something like 40. So explicitly set the height of that button. Okay, cool. So now that we've got that in place, when we hit this button, what I want to do is I want to go and start the replace functionality. Okay, make a little space here. So we need a custom function for that. So I'm going to say void and say replace selected objects like so. Very cool. And so within that button, within this if check, if the button returns true, it's going to call this method now. All right, cool. So now we're going to come down here and we're going to execute all this code. So the first thing that we want to do is we want to check our current selection count because if we don't have anything selected, we shouldn't allow the user or allow the tool to try to do anything else because we can't replace with anything. So we're going to say, if the current selection count is equal to zero, then let's just return. So let's say return. But we should also, because we're making tools, right? And we're not going to be the only person using this tool. We're going to have a lot of end users basically using the tool. We need to tell them what happened. We can't just let it close the window and, you know, have it be done. It's nice to give them information. So what we're going to do is we're going to utilize the editor utility dot display dialog. All right. And just the simple one. Okay, so you can see the arguments for this. It needs a title, a message, and then a string for the button that is on its little dialogue. So for the title, what we're going to do is we're going to say replace objects, warning. And we're going to give a message. So we're going to say, at least one object needs to be selected to replace with exclamation mark. We're serious. And we'll say, okay, for the button. All right. So we'll be able to test this out right away. Okay. So let's jump back into unity now and take a look over here. So we should get our button here once we finish compiling. There we go. So now we've got our button in place. Very cool. So if I were to hit the replace selected objects, we're going to get this display dialog. And it's telling us that we need to select one object, at least one object, so we can replace it with something. And we've got our okay button. So that's perfect. The next check that I want to do down here. So let's let's give ourselves some notes. Say check for selection count, like so. And then we're going to say check for replace objects. Okay, so if we don't actually assign anything to this object field right here, if wanted object is null, then we shouldn't let the tool do anything either. So we're going to say if wanted object, and in this case, we can say if not wanted object. Okay, then let's do the same thing here and just change out that message. Now you could always just write a single method, right? So you don't do all this. You could just, well, why don't I just do it? So we'll say void custom dialog, something like that. And what I'll do is I'll just copy all of this. Put that in there and we'll say string a message like so. And we'll just replace this all with that message will be a lot cleaner this way, a little more pro. All right, so let's copy this whole message right here. And then we can actually just get rid of that and we'll just call custom dialog and pass in our string. It's a little better, a little cleaner, faster, everything. All right, so now what we need to say is we need to say the the replace object is empty. Please assign something. There we go. Cool. So now we didn't have to replicate all that code. It's actually quite cleaner this way. So cool. Let's go and test this out now. Okay, so I'm going to jump back into Unity and we'll select a couple of objects this time, but leave the replace object, object field empty. All right, so we're going to go and select some objects here and say replace objects. And this time we got the warning that says the replace object is empty. Please assign something. So our error checking code is working. So with that, I'm going to close out this particular video. And in the next, what we're going to do is get the replacement functionality in place. So that way we are replacing it with cubes. Let's just do cubes. All right, and we need to pick up the translation and the rotation and scale for all this stuff. So thanks so much.