Search Unity

Bug Error CS0118

Discussion in 'Scripting' started by WizardNinja123, Mar 29, 2023.

  1. WizardNinja123

    WizardNinja123

    Joined:
    Feb 10, 2023
    Posts:
    2
    Hello! I was trying to code an FPS game made by @NattyCreations but I got an error on line 13 saying CharacterController is a type but is used like a variable. Can anyone help me? Here is my code

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

    public class PlayerMotor : MonoBehaviour
    {
    private CharacterController controller;
    private Vector3 playerVelocity;
    public float speed = 5f;
    // Start is called before the first frame update
    void Start()
    {
    CharacterController = GetComponent<CharacterController>(); <------- ERROR
    }

    // Update is called once per frame
    void Update()
    {

    }
    //recieve the inputs for your INputManager.cs and apply them to our character controller.
    public void ProcessMove(Vector2 input)
    {
    Vector3 moveDirection = Vector3.zero;
    moveDirection.x = input.x;
    moveDirection.z = input.y;
    CharacterController.Move(transform.TransformDirection(moveDirection) * speed * time.deltaTime);
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You're trying to use the type name where you should be using your variable.

    Correct code:
    controller = GetComponent<CharacterController>();


    You also have the same problem here:
    CharacterController.Move(transform.TransformDirection(moveDirection) * speed * time.deltaTime);

    It should be:
    controller.Move(transform.TransformDirection(moveDirection) * speed * time.deltaTime);
     
    Bunny83 likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Bunny83 likes this.