error cs0103

i have the folowing error and im busy for over 3 hours trying to fix it but i can`t get it right
error CS0103: The name ‘characterController’ does not exist in the current context

my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class FirstPersonMovement : MonoBehaviour
{
    private CharacterController characterController;

    public float moveSpeed = 10f;
    public float mouseSensitivity = 100f;
    public float jumpForce = 25f;
    public float gravity = -75f;
    public float terminalVelocity = -10f;
    public float jumpHeight = 2f;
    public float sprintMultiplier = 1.5f;

    public KeyCode forwardKey = KeyCode.W;
    public KeyCode backwardKey = KeyCode.S;
    public KeyCode leftKey = KeyCode.A;
    public KeyCode rightKey = KeyCode.D;
    public KeyCode jumpKey = KeyCode.Space;
    public KeyCode sprintKey = KeyCode.LeftShift;
    public float minPitch = -80f;
    public float maxPitch = 80f;

    private float verticalVelocity;
    private float currentSpeed;
    private bool isSprinting = false;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        float horizontal = Input.GetKey(rightKey) ? 1 : Input.GetKey(leftKey) ? -1 : 0;
        float vertical = Input.GetKey(forwardKey) ? 1 : Input.GetKey(backwardKey) ? -1 : 0;

        Vector3 moveDirection = new Vector3(horizontal, 0, vertical);
        moveDirection = Camera.main.transform.TransformDirection(moveDirection);
        moveDirection *= currentSpeed;

        float yaw = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        transform.Rotate(0, yaw, 0);
        if (Camera.main.transform.localEulerAngles.x > 180)
        {
            float x = Camera.main.transform.localEulerAngles.x - 360;
            if (x < maxPitch && x > minPitch)
            {
                float pitch = -Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
                Camera.main.transform.localEulerAngles += new Vector3(pitch, 0, 0);
            }
        }
        else
        {
            if (Camera.main.transform.localEulerAngles.x < maxPitch && Camera.main.transform.localEulerAngles.x > minPitch)
            {
                float pitch = -Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
                Camera.main.transform.localEulerAngles += new Vector3(pitch, 0, 0);
            }
        }

        if (characterController.isGrounded)
        {
            verticalVelocity = -0.5f;
            if (Input.GetKey(jumpKey))
            {
                if (verticalVelocity < jumpForce)
                {
                    verticalVelocity = jumpForce;
                }
            }
        }
        else
        {
            verticalVelocity += gravity * Time.deltaTime;
            if (verticalVelocity < terminalVelocity)
            {
                verticalVelocity = terminalVelocity;
            }
        }

        moveDirection.y = verticalVelocity;
        characterController.Move(moveDirection * Time.deltaTime);

        if (Input.GetKey(sprintKey) && !isSprinting && characterController.isGrounded)
        {
            isSprinting = true;
            currentSpeed *= sprintMultiplier;
        }
        else if (Input.GetKeyUp(sprintKey) && isSprinting)
        {
            isSprinting = false;
            currentSpeed /= sprintMultiplier;
        }
    }
}

if anyone can help it would be amazing thank you to the one who can fix it

I copied your code on a new class and I don’t get this error. Does the console point you to a specific line?

Just, a few things I noticed in this class to improve performance:

  1. Camera.main is expensive. Everytime you call it, unity looks through all objects in the scene to find the main camera. Instead, have a static reference to the main camera at initialization.

  2. Have “Mouse X”, “Mouse Y” strings as constants as to avoid new strings generation during Update(). Strings are expensive.

    const string move_y = “Mouse Y”;
    const string move_x = “Mouse X”;

  3. value multiplied by .1 will give same result as value divided 10, however multiplication is much faster.

You forgot to add public CharacterController characterController; to the beginning of the code. Please “Accept Answer” if this works!