Instantiate and Destroy a GameObject On Input - 2
UnityTranscript
English (Auto-generated)
Hi everyone in this cast we work on functions which will create and destroy the game object in the seeing on key press. So let's open up the appear and disappear on input script And in the cord editor. Let's start off with the make object function first, which will come right here after the update method. So we're going to use avoid because there's nothing to return and make objects. So let's declare the function. Hey, Now this function does two things and number one it checks if an object can be made and number two if yes, then it will create that object in the scene. So first let's start off with an if conditional statement and this if conditional statement checks if an object has already been made if so then exit the function because then we don't need to install an she ate another object clone in that scene. So if object reference not now or I'm just searching for the pipe symbol here. There it is or object to spawn is equal to know, then we want to return. Okay? So if an object has already been made we don't need to do anything, then we just exit out of the function right now if there is no object in the scene and the a key is pressed down, then we want to instill in she ate the object in the scene. And for this we are going to use the instant she ate method which creates a clone of an object at a certain position and rotation. So for this we're going to use the object reference variable that we already declared up here which references a game object. So in this case the cloned game object. And then we're using instant. She ate breath good And insane. She it requires three arguments object position and rotation. And this method doesn't provide any default values for any of these three arguments, the object position or rotation. So any time that we use in stan she ate in a script. We have to provide values for all three of these arguments. And # one. We already created uh this we already declared this variable object to spawn which is a game object. So that's the first argument object to spawn. So the insanity eight function takes as an argument the prefab from which to make a clone from. And the second is the location to spawn the newly installed and she hated clone at which is referenced by the variable spawn location. And if you remember that this spawn location variable is actually a vector three variable. Right? And it has X, Y and Z coordinates. And the last argument to this method is scrutiny in identity. So county. So this is quite training identity. Okay, so this is the default or non value to the objects rotation. And by setting the rotation of this new object or disk loan to this value which is clintonian identity. It's going to ensure that the new object is going to be in its natural orientation. Right? So that's all for the meek object, that's the correct spelling function. Now, let's work on the remove object function, which is responsible for checking if an object can be destroyed and if so then destroying that clone. So let's declare the function once again, avoid your move object. Now let's check if there is an object that can be destroyed and if there's no object to be destroyed, then we exit the function because really there's nothing else to do so in an if conditional statement, right? If the game object which is referenced by the object reference variable, it's now then we want to just exit out of uh this function. So richard right? Else what we want to do is we are going to use the destroy method which is going to destroy or remove the game object from the scene. So else we destroy. Oops. Okay, we don't need that. We don't need any of that. We just need destroy. Not on destroy. So we are going to destroy the object, reference game object. And next, what we're going to do is basically we just set the object reference variable to null once we destroy it. So let's go ahead and control s to save this and let's head back to the scene. I just want to ensure that all this this c sharp script compiles and really there's no error checking little time. Okay, great, So next, what we want to do is we need to track during the course of the game when a player presses down on the A or S. Key and the a key will be used to make a game object in the scene and the S key is used to destroy the game object in the scene. So let's work on this next in the update function. Okay, so we're going to be working here inside of update now. Remember that this update method is called for every frame of the game. So in this method we want to check if the A. Or S. Key has been pressed down. So we are checking input for every frame of the game and out here what we're going to do is so I'll just structure an if, conditional block. Right? So I need an if and I need an L. Safe as well. So else is. Okay, so we're going to use this input dot get key down method which checks if a specific key is pressed down and this is only valid on the first frame that the key is pressed down and not every frame. So here we will say input breezing and put class. So input get key down, that's it, which it takes as an argument, make object key. So if you remember that the key, write the code of the key is referenced by the variable. Make object key. And if the key is pressed down then we want to call the function, make object, which is going to instant she ate a clone a game object on in the scene. Similarly else if the S key is pressed down so else if input dot get key down and out here we have remove object key. So if the S key is pressed down then we want to call the remove object a function that we just worked on, which is going to destroy the cloned object in this scene. Now one last thing that we can do is we can spawn the game object at the start of the scene because really the game object if you see here doesn't exist at the start of our game. And for this we work inside of the start method which is called once at the start of the game and inside of here we can call the make object function which is going to in stand she ate the object clone on the first frame or at the start of the game. So let's go ahead and control s to safe and head back to the scene and um oops. Okay. Now what I'll do is I will click on play so it doesn't seem like we have any errors. So the script is compiling and it's compiled. Okay, so now so we might not see this here because if I come back to the scene and I click on object to spawn so there's a thing that we have to do. So remember we have to drag and drop this script onto the empty game object, which we called object to spawn And out here we have to dragon drop the cube free prefab into this public variables value, which is object to spawn. So out here we're seeing the value of the object to spawn variable is the cube object. And now if you see that the public variable, the vector three variable which we call spawned occassion, which is a vector three and has X, Y and Z coordinates is set to 00 and zero. So basically it would be out here, you can see in the scene and uh basically moving this up and down, it would be Somewhere here. So let's just say -2. I'm just gonna try -2 three. And then let's say uh -2 as well. And just because it's going to be a great cube, which is going to maybe not stand out against these gray rocks, I'm just going to drag and drop this purple material onto the cube prefab to make it a purple cube. Okay, so let's see if we can see the cube at the start of the scene so there we can see it. Okay, so it's right here and if I click on s it disappears, if I click on mm the game object is inst and she hated in the scene once again and maybe what we can do is make this bigger so that we can see it better. And okay, so there you go. Scale, It didn't make much of a difference, but you can see the object here in the middle of the scene. This purple cube. Now look at the left side in the hierarchy window. So when I press down on s the game object or the prefab is destroyed, it's no longer in the scene and when I press on a the clone is in stan she hated and it's visible in the scene and in the hierarchy window. So that's all for this cast.