static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object
Description
Clones the object original and returns the clone.
Clones the object original, places it at position and sets the rotation to rotation, then returns the cloned object.
This is essentially the same as using duplicate command (cmd-d) in Unity and then moving the
object to the given location.
If a game object, component or script instance is passed, Instantiate will clone the entire game object hierarchy, with all children cloned as well.
All game objects are activated.
See Also: In depth Prefab Instantiate discussion
Instantiate is most commonly used to instantiate projectiles, AI Enemies, particle explosions or wrecked object replacements.
using UnityEngine;
using System.Collections;
public class example :
MonoBehaviour {
public Rigidbody projectile;
void Update() {
if (
Input.GetButtonDown(
"Fire1")) {
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation);
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.
var projectile : Missile;
function Update () {
if (
Input.GetButtonDown(
"Fire1")) {
var clone : Missile;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.timeoutDestructor = 5;
}
}
using UnityEngine;
using System.Collections;
public class example :
MonoBehaviour {
public Missile projectile;
void Update() {
if (
Input.GetButtonDown(
"Fire1")) {
Missile clone;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.timeoutDestructor = 5;
}
}
}
import UnityEngine
import System.Collections
class example(
MonoBehaviour):
public projectile as Missile
def
Update():
if Input.GetButtonDown('Fire1'):
clone as Missile
clone = Instantiate(projectile, transform.position, transform.rotation)
clone.timeoutDestructor = 5
After cloning an object you can also use GetComponent to set properties on a specific component attached to the cloned object.
static function Instantiate (original : Object) : Object
Description
Clones the object original and returns the clone.
This function preserves the position and rotation of the cloned object.
This is essentially the same as using duplicate command (cmd-d) in Unity.
If the object is a Component or a GameObject then entire game object including all components
will be cloned.
If the game object has a transform all children are cloned as well. All game objects are activated after cloning them.
See Also: In depth Prefab Instantiate discussion
var prefab :
Transform;
function OnTriggerEnter () {
Instantiate (prefab);
}
using UnityEngine;
using System.Collections;
public class example :
MonoBehaviour {
public Transform prefab;
void OnTriggerEnter() {
Instantiate(prefab);
}
}
import UnityEngine
import System.Collections
class example(
MonoBehaviour):
public prefab as
Transform def
OnTriggerEnter():
Instantiate(prefab)
Note that the Instantiate can clone any type of Object including scripts.