Instantiating new GameObjects and destroying existing ones

Then we will cooldown

Daniel Jennings
3 min readApr 27, 2021

Unity editor value slider

The Unity editor allows you to drag to change numeric values. This allows you to play with a wider set of data values quicker if you don’t know the value that you are looking for.

Unity editor play mode tint

While in play mode you can edit most settings. In the case that you do change the settings in play mode the tint overlay should remind you that copy out the settings you are working with if you don’t want to lose them after exiting play mode.

This tint is a bit subtle but allows for good visibility of being in play mode and values

Instantiating GameObjects

Let’s get to instantiating GameObjects. In a lot of games you will need to create objects at different times. A couple of examples are if you fire a weapon or have enemies spawn in waves.

In this case of the Instantiate method we have 3 parameters. The GameObject to instantiate, the location of the GameObject, and the rotation of the GameObject.

We use a variable to set the GameObject prefab through the Unity editor. The location depends on what we are instantiating. In the case of a weapon it would be in a location related to the weapon. We are not applying a rotation to the instantiated object so we can you just use Quaternion.identity.

Destroying game objects

Once we fire a weapon, and assuming it missed the enemy, it leaves the screen, and it lives forever and ever. Unless we destroy it.

Here we have a method that checks for bounds. If the bounds are passed it will destroy the gameObject it is attached to.

Cooldown system

No one would complain about semi-auto laser fire. However, unfortunately, that laser has to regenerate somehow right??

--

--