Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Object.Instantiate

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function Instantiate(original: Object): Object;
public static Object Instantiate(Object original);
public static function Instantiate(original: Object, parent: Transform): Object;
public static Object Instantiate(Object original, Transform parent);
public static function Instantiate(original: Object, parent: Transform, instantiateInWorldSpace: bool): Object;
public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
public static function Instantiate(original: Object, position: Vector3, rotation: Quaternion): Object;
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
public static function Instantiate(original: Object, position: Vector3, rotation: Quaternion, parent: Transform): Object;
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);

Parameters

original An existing object that you want to make a copy of.
position Position for the new object.
rotation Orientation of the new object.
parent Parent that will be assigned to the new object.
instantiateInWorldSpace If when assigning the parent the original world position should be maintained.

Returns

Object The instantiated clone.

Description

Clones the object original and returns the clone.

This function makes a copy of an object in a similar way to the Duplicate command in the editor. If you are cloning a GameObject then you can also optionally specify its position and rotation (these will default to the original GameObject's position and rotation otherwise). If you are cloning a Component then the GameObject it is attached to will also be cloned, again with an optional position and rotation.

When you clone a GameObject or Component, all child objects and components will also be cloned with their properties set like those of the original object.

By default the parent of the new object will be null, so it will not be a "sibling" of the original. However, you can still set the parent using the overloaded methods. If a parent is specified and no position and rotation is specified, the position and rotation of the original will be used for the cloned object's local position and rotation, or its world position and rotation if the instantiateInWorldSpace parameter is true. If the position and rotation is specified, they will be used as the object's position and rotation in world space.

The active status of a GameObject at the time of cloning will be passed on, so if the original is inactive then the clone will be created in an inactive state too.

For an in-depth tutorial of Instantiate and its uses, visit: In depth Prefab Instantiate discussion.

In this example, we instantiate 10 copies of a prefab object in a line along the x axis.

#pragma strict
public class InstantiateExample extends MonoBehaviour {
	public var prefab: GameObject;
	function Start() {
		for (var i: int = 0; i < 10; i++)
			Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
	}
}
using UnityEngine;

public class InstantiateExample : MonoBehaviour { public GameObject prefab;

void Start() { for (int i = 0; i < 10; i++) Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity); } }

Instantiate can also clone script instances directly. The game object hierarchy will be cloned and the cloned script instance will be returned.

After cloning an object you can also use GetComponent to set properties on a specific component attached to the cloned object.

no example available in JavaScript
using UnityEngine;

public class InstantiateComponentExample : MonoBehaviour { // Instantiate a prefab with an attached Missile script public Missile missilePrefab;

void Start() { // Instantiate the missile at the position and rotation of this object's transform Missile clone = (Missile)Instantiate(missilePrefab, transform.position, transform.rotation); } }

public static function Instantiate(original: T): T;
public static T Instantiate(T original);

Parameters

original Object of type T that you want to make a clone of.

Returns

T Object of type T.

Description

You can also use Generics to instantiate objects. See the Generic Functions page for more details.

In this example, we instantiate our Missile object again, but by using Generics we don't need to cast the result:

no example available in JavaScript
using UnityEngine;

public class InstantiateGenericsExample : MonoBehaviour { public Missile missile;

void Start () { Missile missileCopy = Instantiate<Missile>(missile); } }