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

スクリプト言語

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

GameObject.AddComponent

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 function AddComponent(className: string): Component;
public Component AddComponent(string className);
public def AddComponent(className as string) as Component

Description

ゲームオブジェクトに className という名のコンポーネントクラスを追加します

この関数を使用してその場でオブジェクトの動作を変更します。 またスクリプトクラス名を渡すことでスクリプトを追加することも出来ます。 いくつかのコンポーネントは同じゲームオブジェクトに他のコンポーネントが必須になることがあります。 この関数は、必須なコンポーネントを自動で追加します。例えば、 HingeJointを追加するなら自動的にRigidbodyも追加されます。

	// Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = gameObject.AddComponent ("SphereCollider");
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public SphereCollider sc;
    void Example() {
        sc = gameObject.AddComponent("SphereCollider") as SphereCollider;
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public sc as SphereCollider

	def Example() as void:
		sc = (gameObject.AddComponent('SphereCollider') as SphereCollider)

public function AddComponent(componentType: Type): Component;
public Component AddComponent(Type componentType);
public def AddComponent(componentType as Type) as Component

Description

/componentType/ のタイプからコンポーネントクラスをゲームオブジェクトに追加します。C# ユーザーはジェネリック版を使用することが出来ます

	// Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = gameObject.AddComponent (SphereCollider);
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public SphereCollider sc;
    void Example() {
        sc = gameObject.AddComponent<SphereCollider>();
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public sc as SphereCollider

	def Example() as void:
		sc = gameObject.AddComponent[of SphereCollider]()

RemoveComponentという関数はありません。コンポーネントを削除したい場合は、 Object.Destroyを使用してください。

public function AddComponent(): T;
public T AddComponent();
public def AddComponent() as T

Description

ジェネリック版。詳細については Generic Functions を御覧ください