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

Code showing error: can't implicitly convert type float to int. Are you missing a cast?

Discussion in 'Scripting' started by Leo785, Apr 12, 2020.

  1. Leo785

    Leo785

    Joined:
    Mar 3, 2020
    Posts:
    8
    Guys I don't know how to solve this error and if I have to change my values from float to int then my whole scene will be messed up. Please if anyone can help I'll be much thankful. Code Problem.png
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You can cast x and y to ints explicitly:

    Code (CSharp):
    1. chessPieces[(int)x, (int)y]
    Whether that's a good idea or not depends. Are these going to be perfect integral values? Converting to an int in this way will truncate any fractional part of the number. For example 3.8 would become 3.
     
  3. Leo785

    Leo785

    Joined:
    Mar 3, 2020
    Posts:
    8
    Thank you sir but it looks like I have to change my point values from float to int.
    It'll gonna effect my chess board model greatly but it had to be done because of this error.