Here is the error:
Assets/Scripts/PlayerMovement.cs(17,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)’
Heyho,
Please add code tags to you code. The code will be much more readable and it will add line numbers to it.
The Rigidbody in your code is the rigidbody class itself. You need to use an instance of the Rigidbody class to do something with it.
View the class as the template and the instance as the actual object that is made from the template. (See this tutorial for more about classes: http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes?playlist=17117. It’s an important concept with C#!)
If you gameobject does contain a rigidbody component (you’ll have to add it via the inspector), then the rigidbody component of the gameobject is the actual instance of the Rigidbody class. You just have to get a reference to that component. That’s what the GetComponent (Unity - Scripting API: GameObject.GetComponent) function is for.
So you need to do:
Ok I have added the new code in, but now I have encountered a new error about the CSharpAssembly.dll file, something about it missing.
I haven’t found a single forum thread on this issue so if anyone can Identify where the file is located or what it is that would be greatly appreciated.
Ok, here is the exact error:
Moving Temp/Assembly-CSharp.dll to Library/ScriptAssemblies/Assembly-CSharp.dll:The system cannot find the file described.
Any help would be greatly appreciated
I doubt it has anything to do with the code as my original error had disappeared when I had added everything that you and others had said to add, but here it is anyway:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
There’s a icon bar on top of the editor. The 6th icon from right provides an option to enclose code in code tags, which shows you code like this:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
private Vector3 input;
// Use this for initialization
void Start () {
}
void Update () {
input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
GetComponent<Rigidbody>().AddForce(input * moveSpeed);
}
}
The code looks ok to me. Probably a Unity problem?
Ah ok, thanks for telling me how to do this. I will probably pass this problem onto Unity support as I have found no solution to this. Thank you for helping anyway.