How to add rigidbody, Sphere collider in runtime by JS code?

I attached rigidbody and sphere collider to a character’s palm and feet

When the character and opponent walk to touch each others, the energy bar will reduced to both.

However, i just want when the character fights and kick and reduce energy bar of opponent.

The hierarchy of the character’s left palm of the model is:
Player → Man → Pelvis → Spine → Spine.2 → Heart → L_Shoulder → L_Elbow → L_Hand → L_Palm

How to add rigidbody, Sphere collider to L_Palm in runtime by JavaScript code?

The radius of sphere collider is 0.1

How about creating an enum and setting the state on action(kick, punch etc). Then verify on the state on collision.

do u have sample js code?

erics example here should help :

http://forum.unity3d.com/viewtopic.php?t=26285&highlight=enum

All you should need to do is declare an enum with punch,kick,kiss etc, set the vars on action and then on collision verify the state.

The enum just instruct the code what is the state of a character but NOT teach how to

  1. find the child of a gameobject

Player → Man → Pelvis → Spine → Spine.2 → Heart → L_Shoulder → L_Elbow → L_Hand → L_Palm

  1. How to add rigidbody, Sphere collider to L_Palm in runtime by JavaScript code?

  2. How to set the radius of sphere collider = 0.1

You know the state but cannot locate L_Palm, add rigidbody and collider is no use.

Untested, but should get you close; recommend checking the two reference links as well.

function Start () {
var l_palm = GameObject.Find("
Player/Man/Pelvis/Spine/Spine.2/Heart/L_Shoulder/ L_Elbow/L_Hand/L_Palm");

var palmCollider = l_palm.AddComponent(SphereCollider);
var palmRgbdy = l_palm.Addcomponent(Rigidbody);

palmCollider.radius = .1;

}

Finding Things:

Adding Components:

Thank you for your great teaching, it does work.

However, the radius part has error:

‘radius’ is not a member of ‘UnityEngine.Component’

so, how should I access the radius properties?

The proper syntax is

var palmCollider : SphereCollider = l_palm.AddComponent(SphereCollider); 
palmCollider.radius = 0.1;