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

float to int? error

Discussion in 'Scripting' started by bowandog, Jul 24, 2020.

  1. bowandog

    bowandog

    Joined:
    Jul 24, 2020
    Posts:
    2
    Code (CSharp):
    1.     public int MaxHealth = 10f;
    2.     int currentHealth;
    i have this line of code and
    Code (CSharp):
    1.     public int attackDamage = 10f;
    are getting striked for error CS0266* how can i fix this


    *cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    it is what it says it is

    you can't implicitly convert a float to an int.

    In this, since it's an assignment, you've done "10f", the "f" means "float". Remove the "f" and it's an int.
    Code (csharp):
    1. public int attackDamage = 10;
    If you had a float variable and tried to assign that to an int like so:
    Code (csharp):
    1. float a = 10f;
    2. int b = a;
    You'd also get the same error... to fix this you "cast" it:
    Code (csharp):
    1. float a = 10f;
    2. int b = (int)a;
    Note that when casting from float to int you'd lose any fractional information. Furthermore floats can be out of the range of ints since float.MaxValue is significantly larger than int.MaxValue. It doesn't error out, you just will get weird values back.
     
  3. bowandog

    bowandog

    Joined:
    Jul 24, 2020
    Posts:
    2
    thanks i thought with c# you had to put f after every number. thanks for letting me know because i am new to the whole game development and coding scene
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    Nope, the 'f' denotes that it is a "float". There are several suffixes for common numeric types. As well as implicits.

    implicits:
    int - any number with no fraction part in format ####
    double - any number with fraction part in format ####.####

    explicits:
    double - suffix 'd'
    float - suffix 'f'
    long - suffix 'L'
    ulong - suffix 'UL'
    decimal - suffix 'm' (for money)
    int, but written in binary - prefix '0b'
    int, but written in hex - prefix '0x'

    All other numeric types (short, byte, etc) can be written as 'int' but cast to its type like so:
    Code (csharp):
    1. short i = (short)5;
    Though honestly, you don't even need to put the cast as the compiler has no problem just doing:
    Code (csharp):
    1. short i = 5;
    You can read through these articles which cover the common numeric types:
    integers:
    https://docs.microsoft.com/en-us/do...eference/builtin-types/integral-numeric-types

    floating-point:
    https://docs.microsoft.com/en-us/do...ce/builtin-types/floating-point-numeric-types