Spawning game object in Unity

by Lance Gold

Learning the basics of how to instantiate a prefab object in Unity.

Return to index
Player cube object spanning lots of laser capsule objects

Player cube object spanning lots of laser capsule objects

Instantiate() is used to put a copy of a prefab object onto the Scene.

Here is an example of instantiating an object with the press of the spacebar on the keyboard.

tooltip for VS Input.GetKeyDown

tooltip for VS Input.GetKeyDown

There are KeyCodes. ‘space’ is ‘32’. There are also names. Here are some examples:

KeyCode.Space KeyCode.Tab KeyCode.Alpha1 KeyCode.A KeyCode.Dollar

When dealing with keyboard input, it is helpful to test with the Debug.log(“msg”)


        if(Input.GetKeyDown(KeyCode.Space ))
        {
            Debug.Log("space key pressed")
        }

Dragging the Unity Editor ‘Console’ tab down to make its own window is helpful to view the messages during game runtime.

Console frame added at bottom of Editor

Console frame added at bottom of Editor

Input is working, so next step is to instantiate the GameObject.

Searching the Scripting API, not the manual, here is one link:

Your search for “instantiate” resulted in 198 matches:

Object.Instantiate
Clones the object original and returns the clone.

The process with a spawning a capsule shaped object named “laserPrefab”

(a) Design the new object, a capsule, adding material color and collider and overrides. (done in the steps above)

(b) Add a an “Input.GetKeyDown(KeyCode.Space) { }” in the Update loop to trigger a spawn. Test the input with a “Debug.Log(“key pressed”);” (also done in the steps above)

(c) Create a public (or “[SerializeField]”) variable occupy project memory.

// variable named laserPrefab to hold prefab laser public GameObject laserPrefab;

(d) With a public variable in the “class Player”, down in the component “Player (Script)” shows “Laser Prefab: None(Game Object):

Player Inspector before dragging Laser asset from Prefabs

Player Inspector before dragging Laser asset from Prefabs

(e) Drag the Laser asset from the Prefabs. Delete the Laser object from the Hierarchy Game assets, the “Scene”. You created the Hierarchy Game asset to add the materials and components, after overriding to prefabs, okay to delete the laser from the Scene, so no more lasers visible in the scene. They will begin to appear as the script instantiates them.

After drag and drop of Laser from Assets Prefabs

After drag and drop of Laser from Assets Prefabs

Change the variable access to a SerializedField private variable with an ‘_’:

// variable named laserPrefab to hold prefab laser [SerializeField] private GameObject _laserPrefab;

Drag and drop (again) the Laser prefab Asset to the Inspector Player (Script) Laser Prefab.

(f) now add the “Instantiate” method to create a Laser with a pressed spacebar Key, with a location and rotation. Default rotation uses “Quaternion.identity”.

Instantiate option four with location and rotation

Instantiate option four with location and rotation

if(Input.GetKeyDown(KeyCode.Space )) { Instantiate(_laserPrefab, transform.position, Quaternion.identity); }

“Update” loop, not “Start” loop

While the ship, the game object named “Player” in the Hierarchy Game list is positioned in the “Start” loop, it is initiated when running the game.

public class Player : MonoBehaviour { ... }

The Prefab objects are instantiated, spawned with the press of a keyboard spacebar, so that happens in the “void Update()” loop.