- Home /
GetCompent() adds a Rigidbody
If I call GetCompent() a rigidbody is added. How do I check if the game object has a rigidbody? I do not want to add one.
Well, if it is simply rigidbody, you can just do if(gameObject.rigidbody != null)
Anyway, I dont remember calling GetComponent adding any component... Doesn't it return null if there is no such component attached to the gameobject?
It should just return null, but that is not what is happening. Also, gameObject.rigidbody is depreciated in Unity 5.
Unity will actually edit your script and replace rigidbody with GetComponent().
Wow, that is horrible. Guess I will have to stick to Unity4 for now.
Answer by Slider_j · Apr 21, 2015 at 07:25 AM
if(GetComponent<Rigidbody>()!=null)
should do it IF the script is on the object you are checking for a rigidbody. if you are checking a game object other than the one you are running the script on it would be more like
if(Target.GetComponent<Rigidbody>()!=null)
where Target is a predefined gameobject.
Answer by IrshadKhan_ · Apr 21, 2015 at 02:38 PM
using this script
this.gameObject.AddComponent<Rigidbody>();