2
\$\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\$
  • \$\begingroup\$ Please format your code, there is a format button in the format options. Also, are you getting any compilation errors? \$\endgroup\$ – Moother Apr 25 '15 at 15:42
  • \$\begingroup\$ no,I don't have any compilation errors. \$\endgroup\$ – Bob Apr 25 '15 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 Apr 25 '15 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 May 25 '15 at 18:45
1
\$\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\$
  • \$\begingroup\$ This was my first thought, that the script file isn't Playercontrol.cs to match the code. \$\endgroup\$ – jhocking Jun 24 '15 at 20:41
  • \$\begingroup\$ So file name and class name need to be the same?! for example Main.cs and Main:MonoBehaviour? \$\endgroup\$ – Ewoks Feb 16 '16 at 10:11
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.