Instantiating and Destroying GameObjects in Unity
The capability of creating and removing GameObjects from a Scene - one of the fundamentals of Unity… That is what we are going to be exploring today!
One of the basic and most useful tools that Unity provides us with - allowing us to create enemies, projectiles, powerups and removing them whenever we want them to; for example, when enemies get killed, projectiles collide with other objects, and powerups are picked up.
For this purpose, Unity gives us two Methods to use, one for creation & one for destruction.
Instantiate();
This method creates instances of GameObjects and adds them to the scene.
This is a syntax we will be using today; let’s break it down first.
- Object original - This is GameObject that the method takes and creates a clone. Usually, we use a Prefab of a GameObject here. To read more about Prefabs, click here!
- Vector3 position - This argument defines the position in the scene. If left as a plain ‘Vector3.zero,’ the object will instantiate on top of the parent GameObject.
- Quaternion rotation - This is the object’s initial rotation. To read more about Quaternion, click here!
Destroy();
This method takes a GameObject, component, or asset and completely removes it from the scene. Once this method is called on an object, no further connections can be made with the object.
Let’s use that in praxis!
Assignment: Create a shooting mechanic for the player, which upon pressing the ‘Space’ key, fires a projectile that moves upwards.
Arming our player… with a laser!
To do this, we have to update our ‘Player’ script. We need to let our script know about our laser Prefab. For this, we use a serialized variable.
After that, we need to open up Unity editor and drag out the laser prefab from our project folder into the ‘Laser Prefab’ variable slot in the Player script component on our Player GameObject.
With everything set, all we need to do is instantiate it! In order to keep our code clean, we are going to define a new void method named FireLaser().
In this method, we are going to call the Instantiate() method, and as parameters, we are going to be inserting our laser prefab, followed by a position of our play summed with a new Vector3 with a 0.8 offset on the y axis, so that our laser does not spawn inside of our player… and since we do not need to rotate our laser, just a simple Quaternion.identity. And our method should look like this:
Next, we have to check if a space key has been pressed in our Update method, and if it was, we will call the FireLaser() method.
And we are shooting away! - but wait.. our lasers are just spawning, and nothing else is happening?
And for now, that is correct! - we now have to create a behavior for our lasers. We do that by creating a new C# script called ‘Laser,’ and we will be attaching that to our ‘Laser’ prefab so that all our instantiated instances are behaving the same way.
Gettin’ the laser to move!
To make our laser move, we have to create a transform.Translate command, which will update our projectile's position in each frame. To adjust the speed of our projectile, we will be creating a new serialized variable called _speed.
If we try running our code now, we can see that our lasers are now shooting up; amazing!… Except there is a slight problem - our Hirerachy keeps getting spammed with new and new instances of our Laser prefab, and they never go away.
For that, we need to be calling Destroy() method whenever our lasers leave our screen. For me, that is around 7f on the y-axis. If our lasers get higher than this value, we destroy them!
And that’s that - now we have a functioning laser of death ~ we will try using it on our enemies in the next article!
But that is it for now, thank you for reading and feel free to follow me for more articles — and as always, good luck and see you next time!