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!

more ▼

asked May 31, 2011 at 05:34 AM

eric_c gravatar image

97 41 31 34

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can add:

var otherObj : GameObject = hit.collider.gameObject;
    hit.rigidbody.useGravity = false;

Otherwise it will try to apply to something that doesn't exist.

more ▼

answered May 31, 2011 at 09:18 AM

save gravatar image

9.7k 59 48 90

Thanks for saving my life again dude!

May 31, 2011 at 09:44 AM eric_c

If I knew your life was on the line I would've answered earlier. :)

May 31, 2011 at 09:46 AM save
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: May 31, 2011 at 05:34 AM

Seen: 1903 times

Last Updated: May 31, 2011 at 09:46 AM