error CS0120: An object reference is required to access non-static member
I was following a Youtube tutorial and I've hit a problem. Any Fixes???
***using UnityEngine;
using System.Collections;*
public class Play_Movement : MonoBehaviour {
public static Rigidbody Instance;
public float moveSpeed;
public Vector3 input;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input .GetAxis ("Vertical"));
Rigidbody.AddForce(input * moveSpeed);
}
}**
Answer by Happy-Zomby · Apr 10, 2016 at 06:38 PM
Hi, I believe it should be,
rigidbody.AddForce(input * moveSpeed);
I tried that but it does nothing so I ran the API updater as it told me to and now the script does nothing.
Hi, did you attach this scrpt to the gameobject you want to move?
if you did and still not working you could try this script **using UnityEngine; using System.Collections;
public class Play_$$anonymous$$ovement : $$anonymous$$onoBehaviour {
Rigidbody rb;
public float moveSpeed;
public Vector3 input;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input .GetAxis ("Vertical"));
rb .AddForce(input * moveSpeed);
}
}**
I tried that script but $$anonymous$$onoDevelop doesn't seem to like the "AddForce" section as it is highlighted red???
Answer by Dragonballelf · Apr 10, 2016 at 08:45 PM
Problem Solved Don't Worry, I was being an idiot and forgot to change the 'moveSpeed' once I used the newer code that Unity allows. I hope that makes sense.
using UnityEngine;
using System.Collections;
public class Play_Movement : MonoBehaviour {
public float moveSpeed;
public Vector3 input;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input .GetAxis ("Vertical"));
GetComponent<UnityEngine.Rigidbody>().AddForce (input * moveSpeed);
}
}