original | コピーしたい既存オブジェクト |
position | Position for the new object. |
rotation | Orientation of the new object. |
parent | Parent that will be assigned to the new object. |
instantiateInWorldSpace | 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.. |
Object The instantiated clone.
original のオブジェクトをクローンします
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 you can specify its position and rotation (these 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, again with an optional position and rotation.
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.
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 specified and no position and rotation are specified, 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 specified, 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; 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); } } }
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.
using UnityEngine; using System.Collections;
public class Missile : MonoBehaviour { public int timeoutDestructor;
// ...other code... }
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.rotation);
// Set the missiles timeout destructor to 5 clone.timeoutDestructor = 5; } } }
オブジェクトをクローンした後、さらに GetComponent を使用して、クローンされたオブジェクトの特定コンポーネントのプロパティーを設定できます。
original | Object of type T that you want to clone. |
T T 型のオブジェクト
ジェネリック版も使用できます。詳細については Generic Functions を参照してください。
By using Generics we don't need to cast the result to a specific type.
using UnityEngine;
public class Missile : MonoBehaviour { // ...other code... }
public class InstantiateGenericsExample : MonoBehaviour { public Missile missile;
void Start() { Missile missileCopy = Instantiate<Missile>(missile); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.