言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

Object.Instantiate

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function Instantiate(original: Object): Object;
public static Object Instantiate(Object original);
public static def Instantiate(original as Object) as Object

Parameters

original 複製したい既存オブジェクト
position 新規オブジェクトの位置
rotation 新規オブジェクトの角度

Description

original のオブジェクトをクローンします

この関数はオブジェクトをエディタ上の Duplicate コマンドと同様の方法で複製します。もし GameObject をクローンする場合、さらにその位置および回転を任意で指定できます(各々 Vector3.zero および Quaternion.identity がデフォルト値)。もし Component をクローンする場合、アタッチされているゲームオブジェクトも位置および回転を任意で指定したうえでクローンされます。 GameObject または Component をクローンするとき、全ての子オブジェクトおよびコンポーネントは元のオブジェクトと同様のプロパティの値でさらにクローンします。しかし新規オブジェクトの parent は null となるため、元のオブジェクトの "子" とはなりません。しかし、必要であれば明示的に親を指定することは可能です。さらにクローンするときのゲームオブジェクトの有効状態は次に渡されるため、もし元が無効だった場合、クローンもまた無効状態となります。 See Also: In depth Prefab Instantiate discussion.

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

	var prefab : Transform;

	for (var i : int = 0;i < 10; i++) {
		Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity);
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Transform prefab;
    void Example() {
        int i = 0;
        while (i < 10) {
            Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as Transform;
            i++;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public prefab as Transform

	def Example() as void:
		i as int = 0
		while i < 10:
			(Instantiate(prefab, Vector3((i * 2.0F), 0, 0), Quaternion.identity) as Transform)
			i++

この関数は発射物、AI 付き敵キャラクター、爆発のパーティクル、または破壊されたオブジェクトの Replacement のインスタンス化の場合にもっとも使用されます。

	// Instantiate a rigidbody then set the velocity

	var projectile : Rigidbody;

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

public class ExampleClass : MonoBehaviour {
    public Rigidbody projectile;
    void Update() {
        if (Input.GetButtonDown("Fire1")) {
            Rigidbody clone;
            clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            clone.velocity = transform.TransformDirection(Vector3.forward * 10);
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public projectile as Rigidbody

	def Update() as void:
		if Input.GetButtonDown('Fire1'):
			clone as Rigidbody
			clone = (Instantiate(projectile, transform.position, transform.rotation) as Rigidbody)
			clone.velocity = transform.TransformDirection((Vector3.forward * 10))

この関数はスクリプトのインスタンスを直接クローンすることもできます。 ゲームオブジェクト全体の階層がクローンされ、クローンされたスクリプトのインスタンスが返されます。

	// Instantiate a prefab with an attached Missile script
	var projectile : Missile;

	function Update () {
		// Ctrl was pressed, launch a projectile
		if (Input.GetButtonDown("Fire1")) {
			// Instantiate the projectile at the position and rotation of this transform
			var clone : Missile;
			clone = Instantiate(projectile, transform.position, transform.rotation);
			
			// Set the missiles timeout destructor to 5
			clone.timeoutDestructor = 5;
		}
	}
no example available in C#
no example available in Boo

オブジェクトをクローンした後、さらに GetComponent を使用して、クローンされたオブジェクトの特定コンポーネントのプロパティをセット出来ます。

Description