- Home /
hit.rigidbody.useGravity problem?
Hi, I have a small snippet of script here :
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, radius);
for (var hit : Collider in colliders) {
if (hit.rigidbody)
hit.rigidbody.useGravity = false;
The script works fine if there are only rigidbodies in the overlapsphere, disabling all gravity of the rigidbodies. But, when there are objects that don't have rigidbodies attatched to them in the sphere the other rigidbody in the sphere wont respond to the script ( eg: there are 2 rigidbody and 1 normal game object in the sphere, the rigidbody wont react to the script because there is another gameobject with no rigidbody in the sphere ). It simply returns a MissingComponentExeption "There is no 'Rigidbody' attached to the "BLABLA" game object, but a script is trying to access it." Searched around the forums, no luck, so I decided to post another question here. Please help in JS, thanks!
Answer by save · May 31, 2011 at 09:18 AM
You can add:
var otherObj : GameObject = hit.collider.gameObject;
if(otherObj.GetComponent(Rigidbody)){
hit.rigidbody.useGravity = false;
}
Otherwise it will try to apply to something that doesn't exist.
If I knew your life was on the line I would've answered earlier. :)