4
\$\begingroup\$

I'm a beginner in unity so I got this problem. Here is the script:

using UnityEngine;
using System.Collections;
public class Playercontrol: MonoBehaviour {
    public float maxspeed = 10f;
    bool facingRight = true;

    void FixedUpdate () {
        float move = Input.GetAxis ("Horizontal");

        GetComponent<Rigidbody2D>().velocity
             = new Vector2(move * maxspeed, GetComponent<Rigidbody2D>().velocity.y);

        if (move > 0 && !facingRight){
            Flip ();
        } else if (move < 0 && facingRight) {
            Flip ();
        }
    }

    void Flip() {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;    
    }
}

I don't know why but it keep saying that Can't add script component 'Player control' because the script class cannot be found.Make sure that there no compile errors and that the file name and class name match.

\$\endgroup\$
4
  • \$\begingroup\$ Please format your code, there is a format button in the format options. Also, are you getting any compilation errors? \$\endgroup\$
    – Moo
    Commented Apr 25, 2015 at 15:42
  • \$\begingroup\$ no,I don't have any compilation errors. \$\endgroup\$
    – Bob
    Commented Apr 25, 2015 at 15:49
  • \$\begingroup\$ Please post the file name and class name exactly as they are. This is most probably a mismatch in file name and class name. Your script should have the same name as your class. \$\endgroup\$
    – SanSolo
    Commented Apr 25, 2015 at 16:22
  • 1
    \$\begingroup\$ "Make sure that there no compile errors and that the file name and class name match." Always read your error messages carefully. \$\endgroup\$
    – Almo
    Commented May 25, 2015 at 18:45

3 Answers 3

2
\$\begingroup\$

Check the file name of the script and make sure it's the same as the class name. I've had this problem before after renaming a script through the editor.

\$\endgroup\$
2
  • \$\begingroup\$ This was my first thought, that the script file isn't Playercontrol.cs to match the code. \$\endgroup\$
    – jhocking
    Commented Jun 24, 2015 at 20:41
  • \$\begingroup\$ So file name and class name need to be the same?! for example Main.cs and Main:MonoBehaviour? \$\endgroup\$
    – Ewoks
    Commented Feb 16, 2016 at 10:11
1
\$\begingroup\$

Old question, but I solved this problem using the Help menu and then Reset Packages to defaults.

\$\endgroup\$
0
\$\begingroup\$

You either have a compile error in this script, or you have one in a different script. If this is the first time you're trying to use this script, the entire solution needs to compile before Unity knows about this new class you're introducing.

Locate your error and correct it before trying to add this script. It could be a compile error or something like a name mismatch like SanSolo suggests.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .