Instantiate and Destroy On Input - 1

Transcript

English (Auto-generated)

hi everyone in this cast. We'll work on uh an animation that makes a game object appear and disappear on key press. Or we'll be in stan she ating and destroying and game object on key press if you will. And this scenario can be applied to games for example to spawn. A player at a particular location on key press and then maybe make the player disappear while an enemy character patrols nearby. Okay, so before we start scripting, we're going to set up a few things in our game scene. So first of all, in this hierarchy window, what we want to do is to create a empty game object and we can call this object to spawn. So it's an empty game object. And then again in the hierarchy window well right click and then make another game object. And this time it's a three D. Game object which will simply just be a cube. So basically we're going to be in stan she hating and destroying this cube on key press. So the keys that we're going to use our on a keyboard, the N. S. Keys. Right? So what we want to do now is to drag and drop this cube in the project folder down here which will make this a prefab. And you know that an object is a prefab when it's in the hierarchy window and it's this blue color. So a prefab is a template game object. And what we can now do is we can delete it from this scene because we don't really need it here. Right? So this is the empty. Sorry this is the cube object which is going to be in stan she hated and destroyed on key press and an instance of this cube will be this game object to spawn game object. Okay, so now next what we wanna do is right click here in the assets window and we can create a C sharp script and let's just call it uh appear disappear on input. You could call it anything you want. So basically you could call it substantiate destroy on input or anything else. Okay, so my game uh sorry, my script has been created that's done so out here on the right side and the inspector window you can see not much is going on. Let's just open up this script in the code editor. Right, Okay. So now what we want to do is we want to declare a few public game variables. So just here after the first closing curly brace. Inside the public class appear to disappear on input. Let's declare a public game object. And this is just called object to spawn. Right? So Seaman's are really um Okay, game object, all the names can be different. Doesn't matter. Okay, so we've made a game object which is called object to spawn. Right? And this is a prefab So a prefab is an already made object that can be found in the asset library and not in the scene itself. Think of it as a blueprint for an object and instead creating a Prefab creates a clone of that prefab and that clones properties such as position, rotation and scale can be changed without affecting the original prefab or any other clone. Right then Next we need to declare a variable which will reference the position in the game world, where the Prefab clone will be spawned. And this will be a public variable as well. And that means it's editable in the Unity editor and this is going to be a vector three variable because as you know that game objects in the game scene have X, y and Z coordinates and we will call this spawn location. Okay. And next now we need to declare public variables which will be references to two key codes. A key code is a list of keys that Unity recognizes which are to be used for input. And these are going to be editable in the editor as well as dropdowns. So the first one is your public key code and this is make object key rich is going to be equal to the key code. Right? And next we have another public key code and this is called remove objects. She and this is going to be key code and we'll use the s key to remove a game object. Okay now we will declare a public variable to reference the object that's being created and this is used for checking whether an object can be created or destroyed and this is actually going to be a private variable, so its not editable in the editor and can only be used by within this class. So this is going to be a game object and this is just going to call it an object reference. So we just needed um to reference whether the object is being created or it's destroyed. Right? Okay, So let me move this up and that's all for now, in the next cast, we are going to be working on the make object and remove object functions which are responsible for creating and destroying a game object on key press and everything looks fine so far.
99 Views 0 Likes 0 Comments

In this cast we will learn how to instantiate and destroy a GameObject on keypress. This scenario can be applied to games to spawn a player at a partic...

Comment
Leave a comment (supports markdown format)