Why can't I use Rigidbody2D.velocity ?

A simple question: why can’t I use Rigidbody2D.velocity ? I have to use gameObject.GetComponent().velocity instead.
I get the message “An object reference is required for the non-static field, method or…”
Can anyone explain me why? Thanks in advance :slight_smile:

RigidBody2D is the class from which objects are created. If you want to use it you need an instantiated object.

If you are using Unity 4, you can use rigidBody2D (note the lower case r). However if you are using Unity 5, you need to use GetComponent to access it, for example:

RigidBody2D rigidBody2D = GetComponent<RigidBody2D>();

Some classes have static fields and/or methods, these you can access directly without creating an instance of the class. For example MathF, contains many static methods. But RigidBody2D doesn’t contain any static members/functions, hence the error message.

1 Like

It makes sense now :smile: Many thanks for the reply, I’ll play with it until I get the hang of it ^^