Unity Spawn, Movement & Destroy

Niraj Karki
2 min readSep 9, 2023

Objective: Create a program that spawns a “Minion” (prefab) at the Spawn_Position when you hit the space key. The Minion should then travel to the right of the floor and destroy itself when it reaches the end.

First, create a floor for the minions to stand on and create a GameObject and place it on the leftmost side of the floor where minions will be spawning.

Create a cube GameObject and place it at the right end of the floor with Rigidbody attached to it.

Now create a Border tag and assign it to the cube. We will be using this tag to destroy minion later.

Create a Minion script and write move to right movement logic in update function. Then create a OnTriggerEnter function and check if the object that the minion has touched is the border. If it is the border, then destroy minion.

Now create another script that spawns the minion. Here, create a variable for minion prefab and another for spawn position.

Then, inside the update function, instantiate the minion prefab at spawn position when the player presses the space key.

And this is the final result.