HowTo:: Detect if the gameobject as a rigidbody or not?

Hi there,

Here’s my problem. I need to know if the gameobject my player is trying to hit as a rigidbody on it or not. If not, then return.

something like this…

if(gameobject.transform.rigidbody == null)  //of course this doesn't work but you get the picture :-)
    
          return;
else
        //do code here...

I know it’s a very basic question and the answer might be very trivial but for the past 24 hours, my head seems to think it’s the hardest thing in the world. And the editor keep freezing on me no matter what I try… argggg :-/

thanks,

maparizeau,

Your logic is sound for checking if a rigid body is attached, you should simply be able to do the following

if (gameobject.rigidbody == null) {
  return; // Do nothing because there is no rigidbody attached
} else {
  // Ok we can do our rigidbody code
}

I would imagine that your issue stems from a problem getting your game object reference in the first place. If you post the rest of your code the community will be able to offer more suggestions.

Shawn[QS],

Thanks for putting me back on track. The problem why me code was not working was that I was using

RaycastHit.transform.rigidbody == null

but by putting in

RaycastHit.rigidbody == null

Everything is working fine and I’m not getting any errors.

thanks again.