- Home /
n object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)'
I am a new programmer and I am trying to do this character controller P.S. I am following Youtube video. But I ran into this problem of 'object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)'. Can someone explain to me what the problem is? I'd rather understand the problem then try to solve it myself :) thank you. Here is my code its rather simple.
using UnityEngine; using System.Collections;
public class PlayerMovment : MonoBehaviour { public float movementSpeed;
private Vector3 input;
void Start () {
}
void Update () {
input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
Rigidbody.AddForce(input * movementSpeed);
}
}
Answer by Positive7 · Aug 04, 2015 at 06:12 AM
Rigidbody is UnityEngine component and not the Object Component. You need to reference it like
public Rigidbody rigidBody;
then use it
rigidBody.AddForce(input * movementSpeed);
So the full code would be something like this : public class tryl : MonoBehaviour {
public float movementSpeed;
private Vector3 input;
public Rigidbody rigidBody;
void Update () {
input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
rigidBody.AddForce(input * movementSpeed);
//or
GetComponent<Rigidbody>().AddForce(input * movementSpeed);
}
}
Your answer
Follow this Question
Related Questions
What is the error? 2 Answers
Vector3.SmoothDamp has some invalid arguments 0 Answers
Distribute terrain in zones 3 Answers
Switch statement not working C# (answered) 1 Answer
[Solved]List.FindIndex error C# 1 Answer