How can I make character become a ragdoll with isKinematic?

I’ve been trying to create a script that makes my Player become a ragdoll when you press the space bar. The Player has a ragdoll with each limb having isKinematic to true. Basically, I want to have it so that if you press the space bar, isKinematic for each limb will be set to false so that they become a ragdoll and the script player script called ‘ThirdPersonController’ be deactivated. Also, I was trying to have it so that after 5 seconds, the ragdoll resets to the player by activating the ‘ThirdPersonController’ script and setting isKinematic for the limbs to false. This isn’t working as the Limbs remain with isKinematic set to true, but the boolean itself is being activated, so could somebody please help me with my script? I’m quite new to coding.

Here is my script:

var Limbs : GameObject[];
var IsRagdoll : boolean = false;

function Update(){
	if(Input.GetKeyDown("space")){
		IsRagdoll = true;
	}
}

function RagdollActive(){
	if(IsRagdoll == true);
	{
		Limbs.isKinematic = false;
		GetComponent(ThirdPersonController).enabled = false;
	}
	
	if(IsRagdoll == false);
	{
		Limbs.isKinematic = true;
	}
}

Some points :

  • isKinematic is a variable of the Rigidbody component, not the GameObject component. (this should not even compile)
  • Limbs is an array, so you must loop through each element in the array and modify each
  • the function RagDollActive is never called
  • you should not have a semicolon after an if statement (at line 11)
  • normally variable names are in camelCase, class and components are in UpperCase

You can call a function after a delay period using the Invoke command.

So here is an (untested) script. Please read through the comments to help understand what is happening :

#pragma strict

var limbs : Rigidbody[]; // an array of Rigidbody components (drag and drop in Inspector)
var isRagdoll : boolean = false;

function Update(){
	if(Input.GetKeyDown("space"))
	{
		if(!isRagdoll) // this is the same as writing if (isRagdoll==false)
		{
			RagdollActive();
		}
	}
}

function RagdollActive()
{
	isRagdoll = true; // set ragdoll to true
	// now if space is hit while isRagdoll == true, this function won't be called
	
	for(var limb:Rigidbody in limbs) // for each limb in the array limbs
	{
		limb.isKinematic = false; // set the variable of the Rigidbody component
	}
	
	GetComponent(ThirdPersonController).enabled = false;
	
	// invoke a function after 5 seconds to reactivate object
	Invoke( "RagdollInactive", 5.0 );
}

function RagdollInactive()
{
	for(var limb:Rigidbody in limbs) // for each limb in the array limbs
	{
		limb.isKinematic = true; // set the variable of the Rigidbody component
	}
	
	isRagdoll = false; // set ragdoll back to false
	
	GetComponent(ThirdPersonController).enabled = true;
}