Unity - Scripting API Object - Instantiate
Unity - Scripting API Object - Instantiate
html
unity.com
Search scripting...
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.
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
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:
using UnityEngine;
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;
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);
Instantiate can also clone script instances directly. The entire game object hierarchy will be cloned and the
cloned script instance will be returned.
Version: 2//
0 2 1...other
.3 code... C#
}
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
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 );
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.
using UnityEngine;
void Start()
{
Missile missileCopy = Instantiate<Missile>(missile);
}
}
Did you �nd this page useful? Please give it a rating: unity.com
Version: 2 0 2 1 . 3 C#
i s s u e t r a c k e r. u n i t y 3 d . c o m .