Vincent Taylor

Spawning Game Objects in Unity!

In Unity, spawning objects is a vital part of almost every game. The technical term for doing this is “Instantiation”, or to “Instantiate”.

Spawning A Game Object:

Create a capsule

Save it as a prefab by dragging the GameObject from the Hierarchy panel into the Project panel and dropping it there. I put mine in a folder called “Prefabs”, which is a best practice. You can then delete the Capsule GameObject from the Hierarchy panel, removing it from the scene but leaving it in the project files.

Now that we’ve created a prefab of a simple capsule GameObject, let’s create the script that will instantiate it when the game is running. Right-click in the project panel and choose “Create/C# Script”, then name it. Since I’m creating a space shooter, I’ll call mine “PlayerGun”.

Open the script in your code editor, and delete the Start method.

First, we should create a variable to store the GameObject that we want to instantiate. Inside the class, at the top, create a GameObject variable to hold the Capsule prefab.

To store the prefab, set in Inspector
I renamed my Capsule prefab to Laser Bolt, and set the _capsule variable from the Inspector

Now in the Update method, I want to define when the Laser Bolt will be instantiated. I’ll use “Input.GetButtonDown(“Fire1”)” since it is convenient to use Unity’s Input Manager, where “Fire1” is already assigned to the Mouse 0 button (left click). I could alternatively use “Input.GetKeyDown(KeyCode.Mouse0)”, but that is hard-coded and harder for the player to change.

For cleanliness, I’ll make a seperate function to actually spawn the Laser Bolt prefab.

Now I’m almost at the stage of instantiating. Unity’s Object.Instantiate method has a few different versions I could use, but I want the one that takes

  1. An Object (my Laser Bolt prefab),
  2. A Vector3 spawn position, and
  3. A Quaternion spawn rotation.
Unity Documentation

To avoid physics collision issues, I’ll create a spawn position that is near, but not on, the player’s current position:

The player’s position, but half a unit up

Call the Instantiate method and pass in the _capsule variable, the spawnPoint variable, and set it’s rotation to (0,0,0,0), or “Quaternion.identity”.

The Instantiate method

Now if I save and add this script as a component to the Player GameObject I created in the Simple Player Movement article, I get this:

Instantiating the capsules

Because both the Player and the capsule have physics colliders on them, the Player can bump into the capsules after they’ve been spawned. If I had spawned them at exactly the player’s position there would have been a high chance of them being bumped as soon as they’d been spawned, giving weird movement.

Destroying A Game Object:

“obj” is the Object to be destroyed.

“t” is an optional parameter defining the delay between running this command and the Object actually being deleted in the scene. Usually this is skipped, and left at the default 0.0f (instant destruction).

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer since 8yo (18 years). Currently looking for games industry employment