0% found this document useful (0 votes)
75 views

Unity - Scripting API Object - Instantiate

The document describes the Unity Object.Instantiate method which is used to clone game objects and components at runtime. It can instantiate prefabs, set properties on the clone like position and rotation, and returns the instantiated clone. Examples are provided for instantiating projectiles and particle effects.

Uploaded by

YOOOSE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Unity - Scripting API Object - Instantiate

The document describes the Unity Object.Instantiate method which is used to clone game objects and components at runtime. It can instantiate prefabs, set properties on the clone like position and rotation, and returns the instantiated clone. Examples are provided for instantiating projectiles and particle effects.

Uploaded by

YOOOSE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unity - Scripting API: Object.Instantiate https://docs.unity3d.com/ScriptReference/Object.Instantiate.

html

unity.com

Search scripting...

O b j e c t . I n s t a n t i a t e Manual Scripting API

Leave feedback
Version: 2 0 2 1 . 3 C#
SWITCH TO MANUAL

D e c l a ra t i o n
public static Object I n s t a n t i a t e (Object o r i g i n a l );

D e c l a ra t i o n
public static Object I n s t a n t i a t e (Object o r i g i n a l , Transform p a r e n t );

D e c l a ra t i o n
public static Object I n s t a n t i a t e (Object o r i g i n a l , Transform p a r e n t , bool i n s t a n t i a t e I n W o r l d S p a c e );

D e c l a ra t i o n
public static Object I n s t a n t i a t e (Object o r i g i n a l , Vector3 p o s i t i o n , Quaternion r o t a t i o n );

D e c l a ra t i o n
public static Object I n s t a n t i a t e (Object o r i g i n a l , Vector3 p o s i t i o n , Quaternion r o t a t i o n , Transform
p a r e n t );

P a ra m e t e r s
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.

i n s t a n t i a t e I n Wo r l d S p a c e When you assign a parent Object, pass true to position the new object
directly in world space. Pass false to set the Object’s position relative to
its new parent.

Returns

1z6 07.05.2023, 21:13


Unity - Scripting API: Object.Instantiate https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

O b j e c t The instantiated clone.


unity.com
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
Manual
cloning a GameObject you can specify its position Scripting
and rotation (theseAPI
default to the original GameObject's
position and rotation otherwise). If you are cloning a Component the GameObject it is attached to is also
cloned, 2
Version: again
0 2 1 with
. 3 an optional position and rotation. C#

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

N o t e : When this method clones a child object, it also clones the child's own children. To prevent stack
over�ow, Unity limits this nested cloning. If you exceed more than half your stack size, Unity throws an
InsufficientExecutionStackException .

By default the parent of the new object is null; it is not a "sibling" of the original. However, you can still set
the parent using the overloaded methods. If a parent is speci�ed and no position and rotation are
speci�ed, the original object's position and rotation are 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 are speci�ed, they are used as the object's position and rotation in world space.

The active status of a GameObject at the time of cloning is maintained, so if the original is inactive the
clone is created in an inactive state too. Additionally for the object and all child objects in the hierarchy,
each of their Monobehaviours and Components will have their Awake and OnEnable methods called only if
they are active in the hierarchy at the time of this method call.

These methods do not create a prefab connection to the new instantiated object. Creating objects with a
prefab connection can be achieved using PrefabUtility.InstantiatePrefab.

See Also:

Instantiating Prefabs at run time


PrefabUtility.InstantiatePrefab.

// Instantiates 10 copies of Prefab each 2 units apart from each other

using UnityEngine;

public class Example : MonoBehaviour


{
public GameObject prefab;

2z6 07.05.2023, 21:13


Unity - Scripting API: Object.Instantiate https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

void Start()
{ unity.com
for (var i = 0; i < 10; i++)
{
Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity
}
} Manual Scripting API
}
Version: 2 0 2 1 . 3 C#

Instantiate can be used to create new objects at runtime. Examples include objects used for projectiles, or
particle systems for explosion effects.

using UnityEngine;

// Instantiate a rigidbody then set the velocity

public class Example : MonoBehaviour


{
// Assign a Rigidbody component in the inspector to instantiate

public Rigidbody projectile;

void Update()
{
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1"))
{
// Instantiate the projectile at the position and rotation of this transform
Rigidbody clone;
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);
}
}
}

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

3z6 07.05.2023, 21:13


Unity - Scripting API: Object.Instantiate https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

using UnityEngine; unity.com


using System.Collections;

public class Missile : MonoBehaviour


{
Manual
public int timeoutDestructor; Scripting API

Version: 2//
0 2 1...other
.3 code... C#
}

public class ExampleClass : MonoBehaviour


{
// Instantiate a Prefab with an attached Missile script
public Missile projectile;

void Update()
{
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1"))
{
// Instantiate the projectile at the position and rotation of this transform
Missile clone = Instantiate(projectile, transform.position, transform.rotati

// Set the missiles timeout destructor to 5


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.

D e c l a ra t i o n
public static T I n s t a n t i a t e (T o r i g i n a l );

D e c l a ra t i o n
public static T I n s t a n t i a t e (T o r i g i n a l , Transform p a r e n t );

4z6 07.05.2023, 21:13


Unity - Scripting API: Object.Instantiate https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

D e c l a ra t i o n unity.com
public static T I n s t a n t i a t e (T o r i g i n a l , Transform p a r e n t , bool w o r l d P o s i t i o n S t a y s );

D e c l a ra t i o n
public static T I n s t a n t i a t e (T o r i g i n a l , Vector3 p o s i t i o n , Quaternion r o t a t i o n );
Manual Scripting API
D e c l a ra t i o n
Version: 2 0 2 1 . 3 C#
public static T I n s t a n t i a t e (T o r i g i n a l , Vector3 p o s i t i o n , Quaternion r o t a t i o n , Transform p a r e n t );

P a ra m e t e r s
original Object of type T that you want to clone.

Returns
T Object of type T.

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

By using Generics we don't need to cast the result to a speci�c type.

using UnityEngine;

public class Missile : MonoBehaviour


{
// ...other code...
}

public class InstantiateGenericsExample : MonoBehaviour


{
public Missile missile;

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

5z6 07.05.2023, 21:13


Unity - Scripting API: Object.Instantiate https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Did you �nd this page useful? Please give it a rating: unity.com

Report a problem on this page

Manual Scripting API

Version: 2 0 2 1 . 3 C#

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


Knnoow
wnn IIssssuuee. Please check with the Issue Tracker at

i s s u e t r a c k e r. u n i t y 3 d . c o m .

6z6 07.05.2023, 21:13

You might also like