How do I make and object fall through the floor after a specific time?

When I kill the enemy NPC, I instantiate a ragdoll gameObject with this script attached. I need the ragdoll to fall through the ground after a second, then destroy itself a second later. I have made a new layer, called “Nocollide.” It is layer 10, and in the matrix is set to not collide with any other layers. After it instantiates, I look in the inspector, and it does change the layer to Nocollide, but doesn’t fall through the floor. When I walk into it, I collide with it. If I make the prefab’s layer to Nocollide before I press play, it falls through. I don’t understand why it still collides. Please help. Thank you in advance!

Here is my script:

void Start () 
{
    Destroy(gameObject, 2);
    StartCoroutine(FadeAway());
}

IEnumerator FadeAway() {
    yield return new WaitForSeconds(1f);
    gameObject.layer = 10;
}

You could just implement your own simply little timer. Assuming this script to be in each enemy NPC. Give it a function name Destroy. which does… what you want, but you can do it simpler. Why not remove or disable the physics component on your object after 1 sec. This will still achieve what you want, but not have to deal with layers. Also, instead of destroying you can simply disable the object and re-enable it again when you need the rag doll. If the current rag doll is being used then duplicate it. The thing to note here is Destroying GameObjects or Unity’s Destroy method can be costly if you are doing it pretty frequently.

Hope this helps.

Edit: Also instead of using WaitForSeconds just making your own timer. I say this because it is expensive as well.

float timer = 0.0f;
if( timer >= 1.0f )
{
    // then do something
    timer = 0.0f;
}
timer += Time.deltaTime;

You can use Invoke() to cause something to happen in the future. You can cause something to fall through the floor by turning off its collider. Destroy has an optional second parameter for how far in the future the object should be destoryed. Putting the three together:

#pragma strict 

function Start() {
	Invoke("FallThrough", 2.0);
}
 
function FallThrough() {
		collider.enabled = false;
		Destroy(gameObject, 1.0);
}

This code causes the object to start falling through the floor two seconds after it is created and then it is destroyed one second after that.

I have now figured out the answer. My problem was that I had to change the layer of each child, since on a ragdoll, each limb has a separate collider.