Version: 5.3 (switch to 5.4b)
IdiomaEnglish
  • C#
  • JS

Idioma de script

Selecciona tu lenguaje de programación favorito. Todos los fragmentos de código serán mostrados en este lenguaje.

Object.Instantiate

Sugiere un cambio

¡Éxito!

Gracias por ayudarnos a mejorar la calidad de la documentación de Unity. A pesar de que no podemos aceptar todas las sugerencias, leemos cada cambio propuesto por nuestros usuarios y actualizaremos los que sean aplicables.

Cerrar

No se puedo enviar

Por alguna razón su cambio sugerido no pudo ser enviado. Por favor <a>intente nuevamente</a> en unos minutos. Gracias por tomarse un tiempo para ayudarnos a mejorar la calidad de la documentación de Unity.

Cerrar

Cancelar

Cambiar al Manual
public static function Instantiate(original: Object): Object;
public static Object Instantiate(Object original);
public static function Instantiate(original: Object, position: Vector3, rotation: Quaternion): Object;
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

Parámetros

original An existing object that you want to make a copy of.
position Position for the new object (default Vector3.zero).
rotation El nombre del objeto.

Valor de retorno

Object El nombre del objeto.

Descripción

Devuelve el nombre del objeto.

This function makes a clone of an object in a similar way to the Duplicate command in the editor. If you are cloning a GameObject, or something attached to a GameObject, then you can also optionally specify its position and rotation.

If you are cloning a Component, then the GameObject it is attached to will also be cloned.

When you clone a GameObject or Component, all child objects and components will also be cloned, along with their properties. However, the parent property of the cloned object will be null, so it will not be part of the object hierarchy of the original.

The active status of a GameObject at the time of cloning will be passed, so if the original is inactive then the clone will be created in an inactive state too.

For an in-depth tutorial of Instantiate and its uses, visit: In depth Prefab Instantiate discussion.

In this example, we instantiate 10 copies of a prefab object in a line along the x axis.


        
using UnityEngine;

public class InstantiateExample : MonoBehaviour { public GameObject prefab;

void Start() { for (int i = 0; i < 10; i++) Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity); } }

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

After cloning an object you can also use GetComponent to set properties on a specific component attached to the cloned object.


        
using UnityEngine;

public class InstantiateComponentExample : MonoBehaviour { // Instantiate a prefab with an attached Missile script public Missile missilePrefab;

void Start() { // Instantiate the missile at the position and rotation of this object's transform Missile clone = (Missile)Instantiate(missilePrefab, transform.position, transform.rotation); } }

public static function Instantiate(original: T): T;
public static T Instantiate(T original);

Parámetros

original Object of type T that you want to make a clone of.

Valor de retorno

T Object of type T.

Descripción

Versión genérica. Para más detalles, mira la página de Funciones genéricas.

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 : MonoBehaviour { public Missile missile;

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