I build a fighting game, when I hit the punch button, a collider and rigidbody attached to the palm. After that, I want to destroy both collider and rigidbody. Otherwise, it will keep reduce energy bar even no hit punch button.
function AddColliders()
{
var p_l_palm : GameObject = GameObject.Find("Player/man/Pelvis/Spine/Spine.2/Heart/L_Shoulder/L_Elbow/L_Hand/L_Palm");
var p_r_palm : GameObject = GameObject.Find("Player/man/Pelvis/Spine/Spine.2/Heart/R_Shoulder/R_Elbow/R_Hand/R_Palm");
var p_collider_l_palm : SphereCollider = p_l_palm.AddComponent(SphereCollider);
var p_collider_r_palm : SphereCollider = p_r_palm.AddComponent(SphereCollider);
p_collider_l_palm.radius = 0.1;
p_collider_r_palm.radius = 0.1;
var p_rigidbody_l_palm : Rigidbody = p_l_palm.AddComponent(Rigidbody);
var p_rigidbody_r_palm : Rigidbody = p_r_palm.AddComponent(Rigidbody);
}
function Update () {
Punch();
}
function Punch()
{
if(PunchJoystick.IsFingerDown())
{
Character.animation.Play ("punch");
AddColliders();
}
}
But it throws:
NullReferenceException: Object reference not set to an instance of an object
in this line:
p_collider_l_palm.radius = 0.1;
If I add
Destroy(p_rigidbody_l_palm);
Destroy(p_rigidbody_r_palm);
Destroy(p_collider_l_palm);
Destroy(p_collider_r_palm);
There are no collider and rigidbody to be attached.
How should I fix it?