How to Create and Destroy Objects in Unity
Tutorial example with Lasers
Today we are going to make our player shoot lasers. To do this we need to be able to create objects and destroy them. Creating objects the game is running is called instantiating in Unity.
Setup for example
Create capsule
Adjust scale
Drag game object to Assets to make it a prefab
Prefabs in Unity
Prefabs in Unity are reusable objects. For example you create a dungeon with walls. Creating multiple walls, you create a prefab as a building block to build a large portion of your dungeon with. When you want to create changes to all wall in your game, you only need to change the wall prefab. This will speed up your work significantly and prevent missing updates walls. It’s also possible to change prefab in the scene view and apply the changes back to the prefab (overwrite). A side note:you can still change prefabs in the scene view without affecting the other prefabs. It’s only applied back when you use the overwrite function. (The transform properties are excluded from overwriting)
It’s good practice to prefab everything you are going to use multiple times in your game.
What do we want to do?
Press a key and player shoots laser. Let’s break it down:
If key is pressed
then shoot laser
The condition is easy to understand, but what exactly we need to do to shoot the laser? The laser is an game object so as in the title we need to create a laser every time the fire key is pressed. To learn how to do this just use google. “How to spawn an game object in unity”.
If we analyze what needs to happen to shoot the laser after the button is pressed we end up with:
spawn laser (at player position)
laser moves up
Instantiate an game object
To use the Laser prefab we created we need to be able to reference to it from within our Player script. To do this we create a public variable of type gameobject before Start().
public GameObject LaserPrefab;
Select the Player
Drag Laser prefab from project panel to the newly created inspector input box
Now we can spawn a clone of the laser prefab using the Instantiate function.
Our code looks like:
When read out loud it translates to:
If space key is pressed down
then instantiate game object ‘LaserPrefab’,
at the position of ‘transform.position’ (current position),
with the rotation of ‘Quaternion.identity ’ (current rotation)
Move the laser up
Create a Laser script
Add it to the prefab
Open Laser script
Add a speed variable before the Start()
Use the following code in Update() to move the laser up.
transform.Translate(Vector3.up * _speed * Time.deltaTime);
Add a [Serializefield] to make the speed variable adjustable from the Inspector. Use this to adjust the speed to your liking. Remember the Inspector overwrites the values of the script.
Destroy game objects
All seems to be working fine, but you might have noticed if we keep shooting we get an infinite amount of lasers in our game. This uses resources of the machine unnecessarily.
To prevent that we add some code:
If laser is off screen
destroy laser
Google how to destroy a game object in Unity and with that code we made: