- Home /
Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)
Hello,
I can't seem to wrap the logic around my head between converting a float to an integer. I have looked at all the other Unity answers and still don't seem to get it.
It's this code here that I am having problems with.
Score.GetComponent().score += MainCam.GetComponent().Timer;
Score.GetComponent().score - This is an integer.
MainCam.GetComponent().Timer; - This is a float
IEnumerator PointsTotal()
{
if (CountingPoints == false) {
Score.GetComponent<Score> ().score += ballsLeft;
Score.GetComponent<Score>().score += MainCam.GetComponent<Controls>().Timer;
//Score.GetComponent<Score> ().score += MainCam.GetComponent<Controls>().
yield return new WaitForSeconds (ballsLeft);
CountingPoints = true;
}
}
}
Answer by maccabbe · Jul 11, 2015 at 10:38 PM
The explicit conversion from float to int is
float timer = Time.time;
int score = (int)timer;
so your code could be
Score.GetComponent().score += (int)MainCam.GetComponent().Timer;
I would be worried about using this though because ints cannot have a decimal point while floats like Timer use it. So your code will round Timer to a whole number before adding it to score. You might just want to use a float to store the score and display it as an int instead.
Thank you so much!
I was adding that (int) literally everywhere apart from behind the $$anonymous$$ainCam part.... 0_0
Your answer
Follow this Question
Related Questions
Convert Text to float 3 Answers
Convert a char to int / float 2 Answers
Turn float to int 2 Answers
Error when checking if float is a whole number 2 Answers
Floats and int performance difference? 2 Answers