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. Dismiss Notice

Question Double/Float conversion error.

Discussion in 'Scripting' started by JustYourFriendlyNeighbourhoodSlug, Aug 29, 2020.

  1. JustYourFriendlyNeighbourhoodSlug

    JustYourFriendlyNeighbourhoodSlug

    Joined:
    Aug 24, 2020
    Posts:
    24
    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
    MaxSpeed = 2.5f;
    Player.transform.localScale += ScaleChange;
    PlayerCollider.height = PlayerCollider.height - 1.16; //error here
    }
    if (Input.GetKeyUp(KeyCode.LeftShift))
    {
    MaxSpeed = 5f;
    Player.transform.localScale -= ScaleChange;
    PlayerCollider.height = PlayerCollider.height + 1.16; //error here
    }

    this is a script that I'm using to reduce the height of the player character when it crouches, with the PlayerCollider being a CapsuleCollider reference. I've put the lines with errors in bold, and this is the error they're receiving:
    error CS0266: Cannot implicitly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)

    Can someone pls help fix this (it's meant to reduce/increase the height of the collider when the shift key is pressed/released) along with an explanation of the fix, I'm really new so try to simplify it a bit.
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    In C#, to specify that a value is a float, you put an f at the end. Just like you have with your MaxSpeed float assignment. So change the 1.16 to 1.16f.
     
  3. JustYourFriendlyNeighbourhoodSlug

    JustYourFriendlyNeighbourhoodSlug

    Joined:
    Aug 24, 2020
    Posts:
    24
    thanks, honestly I forget wayyyy to often, and most of the time only put the f in the original definition for the float