Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Script class cannot be found

Discussion in 'Scripting' started by theshushybaka123, Apr 15, 2022.

Thread Status:
Not open for further replies.
  1. theshushybaka123

    theshushybaka123

    Joined:
    Apr 10, 2022
    Posts:
    1
    Hey, so when I try to import this script to the character, it gives me this error: "Can't add script component 'SC_FPSController' because the script class cannot be found", blah blah blah.
    Does anybody know how to fix this?
    Here is the script (yes the name matches with behaviour):
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [RequireComponent(typeof(CharacterController))]
    7.  
    8. public class SC_FPSController : MonoBehaviour
    9. {
    10.     public float walkingSpeed = 7.5f;
    11.     public float runningSpeed = 11.5f;
    12.     public float jumpSpeed = 8.0f;
    13.     public float gravity = 20.0f;
    14.     public Camera playerCamera;
    15.     public float lookSpeed = 2.0f;
    16.     public float lookXLimit = 45.0f;
    17.  
    18.     CharacterController characterController;
    19.     Vector3 moveDirection = Vector3.zero;
    20.     float rotationX = 0;
    21.  
    22.     [HideInInspector]
    23.     public bool canMove = true;
    24.  
    25.     void Start()
    26.     {
    27.         characterController = GetComponent<CharacterController>();
    28.  
    29.         // Lock cursor
    30.         Cursor.lockState = CursorLockMode.Locked;
    31.         Cursor.visible = false;
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         // We are grounded, so recalculate move direction based on axes
    37.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    38.         Vector3 right = transform.TransformDirection(Vector3.right);
    39.         // Press Left Shift to run
    40.         bool isRunning = Input.GetKey(KeyCode.LeftShift);
    41.         float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
    42.         float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
    43.         float movementDirectionY = moveDirection.y;
    44.         moveDirection = (forward * curSpeedX) + (right * curSpeedY);
    45.  
    46.         if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
    47.         {
    48.             moveDirection.y = jumpSpeed;
    49.         }
    50.         else
    51.         {
    52.             moveDirection.y = movementDirectionY;
    53.         }
    54.  
    55.         // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
    56.         // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
    57.         // as an acceleration (ms^-2)
    58.         if (!characterController.isGrounded)
    59.         {
    60.             moveDirection.y -= gravity * Time.deltaTime;
    61.         }
    62.  
    63.         // Move the controller
    64.         characterController.Move(moveDirection * Time.deltaTime);
    65.  
    66.         // Player and Camera rotation
    67.         if (canMove)
    68.         {
    69.             rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
    70.             rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
    71.             playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
    72.             transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
    73.         }
    74.     }
    75. }
    P.S. I use Notepad++ to type scripts, in case you needed to know.
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    are the filename and the class name identical, if not they have to be for unity to pick it up properly
     
    Bunny83, michaelnyh7 and Joe-Censored like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can get this error if there are any compile errors anywhere in your project, because you have to get a successful compile of the entire project before you can add a new script to a gameobject. So the compile error is not necessarily in this specific script. Check the Console for any errors, and fix all of them.
     
    Bunny83, Kakoms and passerbycmc like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,032
  5. SentientTinman

    SentientTinman

    Joined:
    Jan 12, 2019
    Posts:
    1
    So I just ran into this bug, oddly enough just toggling Play mode on and off let me add the script without further problems.
     
    an3344, MClark2010, LordSheep and 3 others like this.
  6. PresidentK5406

    PresidentK5406

    Joined:
    Dec 23, 2023
    Posts:
    1
    Thank you! it is fixed now
     
  7. MClark2010

    MClark2010

    Joined:
    Dec 6, 2021
    Posts:
    1
    Although this post is old, that worked for me just now. Thank you!
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,585
    Please use the "Like" button to show appreciation, don't necro the thread.

    Thanks.
     
Thread Status:
Not open for further replies.