I need to assign a decimal to a variable, but I keep getting errors, and float isn't working.(I'm using unity, and visual studio | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I need to assign a decimal to a variable, but I keep getting errors, and float isn't working.(I'm using unity, and visual studio

here's the code: int hp = 100; float shieldPower = 76.5f; int laserDamage = 15; hp -= (laserDamage - shieldPower); Error message: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists(are you missing a cast?)

19th May 2017, 10:59 PM
KingCodesalot
KingCodesalot - avatar
4 Answers
+ 2
Please share your code and the errors.
19th May 2017, 10:49 PM
Igor B
Igor B - avatar
+ 2
the result of statement hp -= (laserDamage - shieldPower); is float, and the value must stored in float variable. but if you want to store it in int, try to cast (use casting) the result, like that: hp -= (int)(laserDamage - shieldPower);
19th May 2017, 11:15 PM
Ahmed Albarakany
Ahmed Albarakany - avatar
+ 1
Thank you! I think it's working. I'm really new to C#, so all the help is really appreciated.
19th May 2017, 11:22 PM
KingCodesalot
KingCodesalot - avatar
0
my code is now in the description
19th May 2017, 11:00 PM
KingCodesalot
KingCodesalot - avatar