Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Using the Instantiate() function to Spawn Objects
  • Example Code
  • Other Types of Instantiate()

Was this helpful?

  1. Create
  2. Creating Game Objects

Spawning Objects

PreviousCreating Game Objects in the EditorNextUnhiding/Hiding Objects During Gameplay

Last updated 4 years ago

Was this helpful?

Using the Instantiate() function to Spawn Objects

There are 5 different ways (currently) to use the Instantiate() function to create or "spawn" pre-made objects.

The most basic use of it is as follows:

Define the gameObject within the script within the class, but before the Start() function:

In this example, the object is named "thing".

public GameObject thing;

Then, in your script where you want to create or "spawn" the object add this:

Instantiate(thing);

Spawned objects with the simplest version of Instantiate() will spawn with the transform, components, and settings as the original object.

You can put this into a variable:

GameObject copiedThing = Instantiate(thing);

This is helpful when you want to create several in a loop, changing copies individually.

Many game artists use the Instantiate() function with so they don't need to spawn each part individually.

Prefabs for the Instantiate() function are selected from or dragged from the Project window instead of the Hierarchy window.

Example Code

Example of copies of a game object called "thing" spawning each time the player hits the letter S:

using UnityEngine;

public class SpawnThing : MonoBehaviour
{
    public GameObject thing;
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            GameObject copiedThing = Instantiate(thing);
        }
    }
}

In your Hierarchy window, the spawned copies will appear as clones:

Other Types of Instantiate()

Other forms of the Instantiate() function make it easy to control the position and rotation as well as determining the spawned objects' parent object.

REMEMBER: Add the script to a game object in the scene. It will not work if it does not exist in the game.

Check out the and the different ways to use it.

prefabs
Unity Documentation to learn more about the Instantiate() function
Updating Game Objects
Deleting Game Objects