- Home /
Js: UnityEngine.Rigidbody is required, how to fix?
Hello I am attempting to implement Newtonian gravity into unity and I thought I would start by getting the Rigidbody's mass to put into the Newtonian gravity equation. F = G (m_1 m_2)/r^2 ,with m_1 being the mass of the object the script is attached to. However I get an error at the very first stage which is
BCE0020: An instance of type 'UnityEngine.Rigidbody' is required to access non static member 'mass'.
and my script couldn't be simpler.
#pragma strict
function Start () {
GetComponent(Rigidbody.mass);
}
function Update () {
}
My question is, how do I get around the error?
Answer by Graham-Dunnett · Jul 27, 2013 at 11:35 AM
RigidBody.mass is not a Component. RigidBody is. So you need to do:
var myRigidBody : RigidBody = GetComponent(Rigidbody);
However, that's totally unnecessary. All game objects have a member called rigidbody, so you can just do:
function Start () {
DebugLog(rigidbody.mass);
}
Note that the Component is called RigidBody (with an upper-case R and B) and the member is called rigidbody, all lower case. Finally, in your code you clearly want to access the mass member. You should do something with it. In the code I posted I just printed it to the screen.
gregzo beat me to it. His mention of $$anonymous$$onobehaviour is completely spot on.
Answer by gregzo · Jul 27, 2013 at 11:31 AM
If you want to access the rigibody component of a GameObject, the GameObject needs a ... Rigidbody component!
So, add the rigidbody first. Then, no need to use GetComponent ( which you're not calling correctly, by the way ). Simply access with rigidbody.mass ( rigidbody component's are automatically accessible from any Monobehaviour script if one exists ).
Your answer