Space Shooter Tuturial: CS0120 error in script

Hello everyone,

I’m very very new to Unity and working my way through the “Space Shooter” tutorial at the moment. Everything went fine until part 6 where you have to write some code. Mine is identical to the one in the video but mine has an error:
Assets/Scripts/PlayerController.cs(12,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.velocity’

Can anybody help me with this?

When posting compiler errors in the future, please include the code that generates the error. Since this error is so common, I’ll take an educated guess.

Look on line 12 of your PlayerController.cs script. You will see a ‘Rigidbody’ with an upper case ‘R’. You need to use ‘rigidbody’ with a lower case ‘r’. It is very important in both Javascript and C# to get the case right. ‘Rigidbody’ refers to the class ‘rigidbody’ refers to the Rigidbody component on this specific game object.

I encountered the same problem. I’m also completely new to Unity and following the Space Shooter tutorial. The problem appears to stem from the fact that Unity 5 handles things differently.

When typing in the code as it is shown in the tutorial, Monodevelop automatically selects rigidbody with a capital R. This appears not to have any velocity attribute. If you keep the code as it is shown in the tutorial, Unity will not be able to compile it. If you then manually change the capital R in Rigidbody to a lowercase r and save the script and then go back to Unity, Unity will give you a prompt regarding updating the script. Just let Unity update the script and it should work fine. The new code at this stage in the tutorial should then look something like this:

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);
		GetComponent<Rigidbody>().velocity = movement;

Note that the way the rigidbody is addressed to get to the velocity attribute is entirely different.