Can't build, error CS0246: The type or namespace name `MonoBehaviour' could not be found

I’m trying to build a simple code, just for testing purposes, and it won’t let me.
In editor mode the game works fine but when I try to build, these messages show up:
‘Assets/PlayerController.cs(3,33): error CS0246: The type or namespace name MonoBehaviour' could not be found. Are you missing an assembly reference?' 'Assets/PlayerController.cs(4,9): error CS0246: The type or namespace name Rigidbody2D’ could not be found. Are you missing an assembly reference?’

Any ideas?

using UnityEngine;

public class PlayerController : MonoBehaviour {
    public Rigidbody2D rb;
    public float currentSpeed = 0f;
    public float maxSpeed = 5f;
    public float jumpPower = 10f;
    void FixedUpdate()
    {
        Move();

    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Space)) Jump();
        if (Input.GetKey(KeyCode.LeftShift)) currentSpeed = Input.GetAxis("Horizontal") * maxSpeed * 2;
        else currentSpeed = Input.GetAxis("Horizontal") * maxSpeed;

    }

    void Jump()
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpPower);
    }

    void Move()
    {
        rb.velocity = new Vector2(currentSpeed, rb.velocity.y);
    }
}

You might find this thread useful…

Sadly my error isn’t similar to that, I just can’t build at all, it’s like it’s not recognizing I’m using unity.engine

Good news, I just updated to 5.6.1 and it got fixed! So, thanks for the update. <3

oky

Try Right-clicking the script in Unity and then click Reimport

1 Like

that isn’t there i cant see that on right clicking

Not sure if this helps (Maybe too late or not right answer), but you’re missing the first two “using’s”; “using System.Collections” and “using System.Collections.Generic”. They should be the very first two lines of code there.

1 Like

Thx man that helped me a lot :wink:

Sorry for replying to an old question, but if somebody is still facing this problem, make sure your first line is this in your script:
using UnityEngine;

1 Like

i have just create new scripts in this projects and all begin working

1 Like

Thanks, it worked for me too.