Instantiating & Destroying Game Objects

Angela Jones-Wright
2 min readMay 16, 2021

Instantiating

In Unity’s coding, Instantiate calls for a duplicate object to appear and used while playing. By saying, “ Hey I need a laser to be fired then I need it to do this when it comes into contact with an object.”

Let us looks at the code. The function is calling a laser to fire a shot. But if we look below we are calling for our prefab TripleShot to fire. So it will duplicate this object and release it out in the game. Then we are telling to telling how far the prefab can go.

Destroying Game Objects

Destroying game objects is a bit easier. The code to the left is telling the game if the player hits the enemy to take away one life. Once the player has used all lives and then to destroy the player. The code that is uses Destroy(this.gameObject);. This line is telling that the game object is done and can be deleted from the scene.

So we have talked about how to write code for a player, deal with prefab objects, and make them disappear after they are not needed. Is there something else to help make this code run with unity?

The box collider has to communicate with this bit of script.

--

--