Vous êtes sur la page 1sur 4

7/10/2017 Unity - Scripting API: Object.

Instantiate

Object.Instantiate
Other Versions

public static Object Instantiate(Object original);


public static Object Instantiate(Object original, Transform
parent);
public static Object Instantiate(Object original, Transform parent,
bool instantiateInWorldSpace);
public static Object Instantiate(Object original, Vector3 position,
Quaternion rotation);
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 Pass true when assigning a parent


Object to maintain the world
position of the Object, instead of
setting its position relative to the
new parent. Pass false to set the
Object's position relative to its
new parent.

Returns
Object The instantiated clone.

Description
Clones the object original and returns the clone.

Search scripting...
This function makes a copy of an object in a similar wayunity3d.com
to the
Duplicate command in the editor. If you are cloning a GameObject
Manual Scripting API then you can also optionally specify its position and rotation (these
default to the original GameObject's position and rotation
otherwise). If you are cloning a Component then the GameObject it
Version: 2017.1 (switch to 2017.2b)
Scripting API C#
is attached to will also be cloned, again with an optional position
JS

and rotation.
UnityEngine SCRIPT LANGUAGE
When you clone a GameObject or Component, all child objects and
UnityEngine.Advertisements
Selectwith
components will also be cloned yourtheir
preferred scripting
properties setlanguage.
like thoseAll
UnityEngine.AI code snippets will be displayed in this
of the original object.
language.
UnityEngine.Analytics
U it E i A i ti
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html 1/4
7/10/2017 Unity - Scripting API: Object.Instantiate
UnityEngine.Animations
By default the parent of the new object will be null, so it will not be
UnityEngine.Apple a "sibling" of the original. However, you can still set the parent
UnityEngine.Assertions using the overloaded methods. If a parent is speci ed and no
position and rotation is speci ed, the position and rotation of the
UnityEngine.Audio
original will be used for the cloned object's local position and
UnityEngine.CrashReportHandler
rotation, or its world position and rotation if the
UnityEngine.Events instantiateInWorldSpace parameter is true. If the position and
UnityEngine.EventSystems rotation is speci ed, they will be used as the object's position and
UnityEngine.Experimental rotation in world space.

UnityEngine.iOS
The active status of a GameObject at the time of cloning will be
UnityEngine.Networking passed on, so if the original is inactive then the clone will be
UnityEngine.Playables created in an inactive state too.
UnityEngine.Pro ling
See Also: In depth Prefab Instantiate discussion.
UnityEngine.Purchasing
UnityEngine.Rendering
UnityEngine.SceneManagement // Instantiates 10 copies of prefab each 2 units ap

UnityEngine.Scripting
using UnityEngine;
UnityEngine.Serialization using System.Collections;
UnityEngine.SocialPlatforms
UnityEngine.Sprites public class ExampleClass : MonoBehaviour
{
UnityEngine.TestTools
public Transform prefab;
UnityEngine.Timeline void Start()
UnityEngine.Tizen {
UnityEngine.U2D for (int i = 0; i < 10; i++)
{
Instantiate(prefab, new Vector3(i * 2.0
}
}
}

Instantiate is most commonly used to instantiate projectiles, AI


Enemies, particle explosions or wrecked object replacements.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {


public Rigidbody projectile;
void Update() {
if (Input.GetButtonDown("Fire1")) {
Rigidbody clone;
clone = Instantiate(projectile, transfo
clone.velocity = transform.TransformDir
}
}
}

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

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html 2/4
7/10/2017 Unity - Scripting API: Object.Instantiate

using UnityEngine;
using System.Collections;

public class Missile : MonoBehaviour


{
public int timeoutDestructor;

// ...other code...
}

public class ExampleClass : MonoBehaviour


{
// Instantiate a prefab with an attached Missil
public Missile projectile;

void Update()
{
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1"))
{
// Instantiate the projectile at the po
Missile clone = (Missile)Instantiate(pr

// Set the missiles timeout destructor


clone.timeoutDestructor = 5;
}
}
}

After cloning an object you can also use GetComponent to set


properties on a speci c component attached to the cloned object.

public static T Instantiate(T original);


public static T Instantiate(T original, Transform parent);
public static T Instantiate(T original, Transform parent, bool
worldPositionStays);
public static T Instantiate(T original, Vector3 position, Quaternion
rotation);
public static T Instantiate(T original, Vector3 position, Quaternion
rotation, Transform parent);

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.

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html 3/4
7/10/2017 Unity - Scripting API: Object.Instantiate

In this example, we instantiate our Missile object again, but by


using Generics we don't need to cast the result:

using UnityEngine;

public class InstantiateGenericsExample : MonoBehav


{
public Missile missile;

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

Leave Feedback

Is something described here not working as you expect it to? It might be a Known

Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.

Copyright 2017 Unity Technologies. Publication: 2017.1-001Q. Built:

2017-08-29.

Tutorials Community Answers Knowledge Base Forums

Asset Store

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html 4/4