Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Cannot implicitly convert type 'float' to 'UnityEngine.Character help

Discussion in 'Scripting' started by Gigget, May 21, 2020.

  1. Gigget

    Gigget

    Joined:
    May 20, 2020
    Posts:
    9
    this is the code and I just started and cant figure out how to fix it

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class crouch : MonoBehaviour
    {
    CharacterController characterCollider;
    // Start is called before the first frame update
    void Start()
    {
    characterCollider = gameObject.GetComponent<CharacterController> ();
    }
    // Update is called once per frame
    void Update()
    {
    if (Input.GetKey(KeyCode.LeftControl) ) {
    characterCollider.height = 1.0f;
    }
    else
    {
    characterCollider = 3.8f;
    }
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    If you look closely at your error message it will tell you the line number (and even column number in the line) of the error. That will take you right to the problem! In your case:
    Code (CSharp):
    1. characterCollider = 3.8f;
    should be
    Code (CSharp):
    1. characterCollider.height = 3.8f;
    The error message is telling you that you're trying to assign a float value to a variable that is supposed to hold a CharacterController. That's because your variable "characterCollider" is of type "CharacterController", but the value you are trying to assign, 3.8f, is a float (short for 'floating point number')
     
  3. Gigget

    Gigget

    Joined:
    May 20, 2020
    Posts:
    9
    thank you it got rid of the error but my crouch dosent work