I am trying to add a special part to my game where when i “Kill” the enemy he will have a rigid-body attached to him then after five seconds the enemy is destroyed. I am new to coding and i thought that this code would work but when i tried it tragically failed
#pragma strict
var Health = 100;
function Update ()
{
if(Health <= 0)
{
AddComponent();
}
}
function ApplyDamage (Damage : int)
{
Health -= Damage;
}
function AddComponent (className : String) : Component
{
gameObject.AddComponent ("FoobarScript");
var sc : SphereCollider;
sc = gameObject.AddComponent ("Rigidbody");
yield WaitForSeconds (5);
Dead();
}
function Dead ()
{
Destroy (gameObject);
}
The Original code that worked
#pragma strict
var Health = 100;
function Update ()
{
if(Health <= 0)
{
Dead();
}
}
function ApplyDamage (Damage : int)
{
Health -= Damage;
}
function Dead ()
{
Destroy (gameObject);
}