Error CS0120-Object Reference is required to access a non-static memberUnityEngine.Rigidbody.Velocity
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
Rigidbody.velocity = movement * speed;
}
}
For future questions, please use the 101010 button to format your code. I did it for you this time. Also, please don't use all upper case in your title. No need for yelling. :p
Answer by Dave-Carlile · Aug 28, 2015 at 06:51 PM
The problem is on your last line: Rigidbody.velocity...
Most programming languages are case sensitive, meaning the case of the letters matter. Rigidbody is not the same as rigidbody.
In Unity, class names generally being with an upper case letter, while properties begin with a lower case letter. In this case you want to use rigidbody.velocity =
in order to reference the property. When you do Rigidbody.velocity =
the compiler thinks you're trying to set a static property in the Rigidbody
class, and no such property exists. The compiler does find the instance property called velocity
though so it give you the error that you're trying to access that non-static property as if it were static.