Legacy Documentation: Version 5.0
Language: English
  • 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

Sumbission failed

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

Close

Cancel

Switch to Manual
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): Object;
public static Object Instantiate(Object original);

Parameters

originalAn existing object that you want to make a copy of.
positionPosition for the new object.
rotationOrientation of the new object.

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 Vector3.zero and Quaternion.identity respectively). If you are cloning a Component then the GameObject is 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. However, 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 explicitly if you wish. Also, 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.

See Also: In depth Prefab Instantiate discussion.

#pragma strict
// Instantiates 10 copies of prefab each 2 units apart from each other
public var prefab;
function Start() {
	for (var i = 0; i < 10; i++) {
		Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
		i++;
	}
}
	// Instantiates 10 copies of prefab each 2 units apart from each other

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform prefab; void Start() { for (int i = 0; i < 10; i++) { Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity); i++; } } }

Instantiate is most commonly used to instantiate projectiles, AI Enemies, particle explosions or wrecked object replacements.

	// Instantiate a rigidbody then set the velocity

var projectile : Rigidbody;

function Update () { // Ctrl was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform var clone : Rigidbody; clone = Instantiate(projectile, transform.position, transform.rotation); // Give the cloned object an initial velocity along the current // object's Z axis clone.velocity = transform.TransformDirection (Vector3.forward * 10); } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Rigidbody projectile; void Update() { if (Input.GetButtonDown("Fire1")) { Rigidbody clone; clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody; clone.velocity = transform.TransformDirection(Vector3.forward * 10); } } }

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

	// Instantiate a prefab with an attached Missile script
	var projectile : Missile;

function Update () { // Ctrl was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform var clone : Missile; clone = Instantiate(projectile, transform.position, transform.rotation); // Set the missiles timeout destructor to 5 clone.timeoutDestructor = 5; } }
no example available in C#

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


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

Parameters

Description

Generic version. See the Generic Functions page for more details.