"An object reference is required for non-static field, method, or property" (134016)

I started experimenting with Unity only today. I was going through a video project tutorial “Roll-a-Ball” and I got this error in the following script. The code is exactly as shown in the tutorial. I cannot figure out what’s wrong with this. Help, anybody, please?

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
	void FixedUpdate()
	{
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

		Rigidbody.AddForce(movement);
			 
	}
}

To use a non static method you need an object of that type. Right now you are using a class type instead of an object of that type. For instance you can do the following int x=0; string text=x.ToString(); but cannot do the following string text=int.ToString();

Yes. I tried doing that. I did this. It didn't work. Rigidbody rigidbody; rigidbody.AddForce(x,y,z);

1 Answer

1

The roll-a-ball tutorial was written for Unity 4. I’m guessing you’re using Unity 5? If so:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

  Rigidbody rigidbody;

  void Start()
  {
      rigidbody = GetComponent<Rigidbody>();

  void FixedUpdate()
  {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rigidbody.AddForce(movement);
  }
}

Thanks a lot. It worked. And yes, I am using Unity 5