Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
Please format your code, there is a format button in the format options. Also, are you getting any compilation errors? – MrSnappingTurtle Apr 25 at 15:42
    
no,I don't have any compilation errors. – Bob Apr 25 at 15:49
    
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. – SanSolo Apr 25 at 16:22
1  
"Make sure that there no compile errors and that the file name and class name match." Always read your error messages carefully. – Almo May 25 at 18:45

2 Answers 2

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.

share|improve this answer
    
This was my first thought, that the script file isn't Playercontrol.cs to match the code. – jhocking Jun 24 at 20:41

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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