Timing scipt not working. Why? (More Info In Thread)

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);
}

http://forum.unity3d.com/threads/143875-Using-code-tags-properly

–Eric

Thank you my post has been fixed.

Just simply change AddComponent

function Ragdoll()
{
    gameObject.AddComponent ("FoobarScript");
    var sc : SphereCollider;
    sc = gameObject.AddComponent ("Rigidbody");
    yield WaitForSeconds (5);
    Dead();
}

and change

 if(Health <= 0)

    {

        AddComponent();

    }

to

 if(Health <= 0)

    {

        Ragdoll();

    }

I wouldnt waste my time with a co-routine anyway.

Destroy comes in another flavour which takes the second parameter as a time till destroy…

var isDead : boolean = false;
function Update()
{

  if(!isDead)
  {
    if(Health <= 0)
    {
       gameObject.AddComponent ("FoobarScript");
       gameObject.AddComponent ("Rigidbody");
       isDead = true;
       
       Destroy(gameObject, 5);
    } 
  }
  
}