Ragdoll Doesn't Destroy??

I have a script here that give the enemy 100 hitPoints, and if it dies, the enemy will be destroyed and replaced with a ragdoll at the same coordinates and angle it died at (From the FPS Tutorial).

Ive added a small code to it so that the ragdoll will be destroyed too after 4 seconds, however it doesn’t destroy.

var hitPoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;

function ApplyDamage (damage : float) {
	// We already have less than 0 hitpoints, maybe we got killed already?
	if (hitPoints <= 0.0)
		return;

	hitPoints -= damage;
	if (hitPoints <= 0.0)
	{
		Detonate();
	}
}

function Detonate () {
	// Destroy ourselves
	Destroy(gameObject);
	
	// Play a dying audio clip
	if (dieSound)
		AudioSource.PlayClipAtPoint(dieSound, transform.position);

	// Replace ourselves with the dead body
	if (deadReplacement) {
		var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
		
		// Copy position  rotation from the old hierarchy into the dead replacement
		CopyTransformsRecurse(transform, dead);
		
		yield WaitForSeconds (4);
		Destroy (deadReplacement);
	}
}

static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
	dst.position = src.position;
	dst.rotation = src.rotation;
	
	for (var child : Transform in dst) {
		// Match the transform with the same name
		var curSrc = src.Find(child.name);
		if (curSrc)
			CopyTransformsRecurse(curSrc, child);
	}
}

Ive added in the code :

yield WaitForSeconds (4);
Destroy (deadReplacement);

And the rest from the tutorial, but it doesn’t seem to destroy the ragdoll, anyone know why?

Anyone?

They say Destroy can be given a duration. Try removing the yield and add 4 to Destroy ?

(I haven’t done this tutorial (yet) so I can’t be of any great help. Ragdoll is a component ?)

I dont think thats the issue, because I also tried removing the WaitForSeconds Line and the ragdoll didn’t destroy at all either…

EDIT: Nope, no luck, i tried Destroy (rigidbody,4); aswell, but it didnt destroy…

EDIT2: I think the problem is Destroy (Transform,2); That should work but i get this error:

Assets/WeaponScripts/CharacterDamage.js(32,25): BCE0023: No appropriate version of ‘UnityEngine.Object.Destroy’ for the argument list ‘(System.Type, int)’ was found.

When you want to refer to the component Transform, shouldn’t you be using transform instead of Transform ?

But I don’t think trying to remove the Transform component can solve it. If you want to remove a standard component (or even a custom), try using GetComponent ?

I don’t think you can even destroy the transform component. Its a required component for a game object. So either you remove the whole game object or a specific component other than transform

The only reason I tried using destroy(transform) is because the ragdoll IS a transform, atleast thats what it says it is, but nothing seems to work, ive tried both Transform and transform, although i havent tried getcomponent, what other options do it have?

I thought about destroying the collider of the enemy so they just fall through the ground but thats a little less efficent…

If you simply want to get rid of a gameObject, I guess Destroy (gameObject) should do it ?

Tried it, still doesnt work… Im suspecting destroy(Transform) will work, however the parameters of the detonate function are wrong, and i dont quite understand parameters

mode more serious on

  • Move
// Destroy ourselves
   Destroy(gameObject);

from the top to the bottom of Detonate function.

  • Remove those lines
      yield WaitForSeconds (4);
      Destroy (deadReplacement);
  • Create a javascript file, called SelfDestruct with those lines :
var countdown : float = 4.0;

function Start () {
	Destroy (gameObject, countdown);
}
  • Add the javascript component previously made on the gameObject you choose for deadReplacement in the Inspector

Does it work now ?

Didnt work, apparently if you try to destroy gameobject, it just destroys the initial enemy that can walk and attack, NOT the ragdoll, and thats what im trying to do, destroy the ragdoll…

This really doesn’t make any sense, how come its not possible for a ragdoll to be destroyed >=(

EDIT: Well idk why i didnt think of this before, but all i did was add this script:

var countdown : float = 4.0; 

function Start () { 
   Destroy (gameObject, countdown); 
}

to the inital ragdoll and thats was it, it worked… Thanks for the help guys :slight_smile:

You are destroying the prefab (deadReplacement) when you want to destroy the current instantiation of said prefab (dead).

however on top of that you don’t want to destroy the transform component, but rather the entire gameObject, so I’d suggest using Destroy(dead.gameObject);

Oh wow, thats so much easier ntero, thats SOO much, thats much more efficent, thats a lot :slight_smile: