- Home /
An object reference is required for non-static field, method, or property
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 ins$$anonymous$$d 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);
Answer by tanoshimi · Mar 22, 2015 at 10:17 PM
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);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer
Persistent mouselook disabling error? 0 Answers
Distribute terrain in zones 3 Answers
I need help referencing another script so I can disable it (C#) 1 Answer