ゲームオブジェクトに 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)
/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を使用してください。
ジェネリック版。詳細については Generic Functions を御覧ください