Do I have to do something in advance before I do "public class MyClass : MonoBehaviour () {}"?

I was doing the Roll-A-Ball project and I entered in the code at the bottom of the page to roll the ball. I did watch the video and I did everything it said to do for setting up. After I finished copying in the code I saved it and clicked play. The ball did nothing when I pressed the arrow keys. I checked the console and it said this, twice.

“The referenced script on this behaviour is missing.”

Here is the code:

using UnityEngine;

using System.Collections;

public class PlayerController : MonoBehaviour {

public float speed;

private Rigidbody rb;

void Start ()
{
    rb = GetComponent<Rigidbody>();
}

void FixedUpdate ()
{
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce (movement * speed);
}

}

48621-assignnewmonobehaviour.png

Click the selector next to your GameObject components script field. You can also drag your MyClass.cs scriptfile directly into the field. Thirdly, you can create a new component from your script via the AddComponent button and typing the name of your script or by simply dragging the MyClass.cs script file into the blank area in the inspector below.

Your error most likely happened, because at some time you changed the name of the class or script file without renaming the other. Or because you had compile errors that prevented Unity from recognizing your class. Then the reference between the script field of the component and the actual script file gets lost and needs to be reassigned. A common issue for beginners, no worries.