Unity3D – AddComponent – Javascript

AddComponent to THIS GameObject

Create a Cube and attach:


#pragma strict

function Start() {
        // Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = gameObject.AddComponent ("SphereCollider");
	}
	
function Update(){
	}

Play and watch Cube params into Inspector

AddComponent to ANOTHER GameObject

Create a Cube

Create an EmptyObject and attach:


#pragma strict

private var cubeobj : GameObject;

function Start() {
	cubeobj = GameObject.Find("/Cube"); // Find gameobject with name

        // Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = cubeobj.AddComponent ("SphereCollider");
	}
	
function Update(){
	}