Rigidbody Issue

I have 2 types of rigidbodies in my game, the enemies and the bullets you fire. I want a script that checks if they are lying complete still, and if so, makes the sleep forever, to reduce the lag when they start piling up. Any ideas how to do this?

If you want the rigidbody to sleep forever, then you probably don't need the rigidbody itself anymore, so you can make a small script and attach it:

function OnCollisionStay () {
    if (rigidbody.IsSleeping) {
    	Destroy(rigidbody);
    	Destroy(this);
    }
}

edit: Forget about this answer, Eric's is much better. Read the comments. ;)

Well, the simplest way to do it the way you need is setting up a script to check if the rigidbody.velocity is zero for over 4 seconds or so, then "sleep".

You could also add another variable on the OnCollisionStay to say if it's grounded or not, so it would only sleep if 4 seconds AND grounded. For this, since it's just bullets, you could put it on the same script and do something like this:

private var isGrounded = false;

function OnCollisionStay (collisionInfo : Collision) {
    for (var contact : ContactPoint in collisionInfo.contacts) {
    	isGrounded = false;
    	if (contact.otherCollider.rigidbody != null) {
    		if (contact.otherCollider.tag == "ground") {
    			isGrounded = true;
    		}
    	}
    }
}

function OnCollisionExit () {
    isGrounded = false;
}

In this example you'd need to tag every ground object with the tag "ground".