- Home /
Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)
I have this error on my script that I need help fixing. There are many topics on this but they look nothing like my code and I don't understand what's the problem here. Here's my script, please help:
public class Go : MonoBehaviour {
private float guiTime;
private float initialtime;
private int restSeconds;
int CountSeconds;
int CountMinutes;
private int seconds;
private int minutes;
private string texttime;
void Update (){
initialtime = Time.time;
CountSeconds = CountSeconds + (CountMinutes + 60);
guiTime = Time.time - initialtime;
restSeconds = CountSeconds = guiTime;
seconds = restSeconds % 60;
minutes = restSeconds / 60;
texttime = string.Format("(00:0):(00:3)", minutes, seconds);
GetComponent<GUIText>().text = texttime;
if(seconds <= 3 && minutes <= 3)
{
//Pause the game
Time.timeScale = 3;
}
}
}
The error occurs where it says, restSeconds = CountSeconds = guiTime;, or on line 20 if you are using Unity.
Answer by Lovrenc · May 31, 2014 at 10:43 PM
Explanation of error. Maybe you should try to understand it instead of getting others to do stuff for you.
CountSeconds = CountSeconds + (CountMinutes * 60); //You should multiply here not sum
//But what is the point? you override this anyways and never use this value
Your error is here:
restSeconds = CountSeconds = guiTime;
restSeconds and CountSeconds are both of type integer, while guiTime is of type float. Since float hold MORE information than integer, compiler wont allow implicit conversion, since that would mean you would lose information and that would lead to more programming errors. You can do:
change those two varaibles to float
cast float to int with
(int)guiTime
This will change value to integer, it will be truncated though.
"$$anonymous$$aybe you should try to understand it ins$$anonymous$$d of getting others to do stuff for you." Haha thanks for assu$$anonymous$$g I'm too lazy to try to understand it. If I wanted others to do stuff for me, then I would hire people to make my game for me, but I don't. Thanks anyways your suggestions fixed the error. Bye now.
I saw you posted this snippet in other questions. Also, while scripts you found when looking for error message are different than yours, this is such general problem i am sure you would get it if you just forced yourself to read trough it. Combined with error line number it would be a breeze.
Also, I suggest that after you finish this script you try to make it cleaner and simpler. It would be a neat exercise.
Haha thanks for assu$$anonymous$$g I'm too lazy to try to understand it
Never seen anyone take it as a compliment :D Good man!
Lovrenc, it's called Unity Answers for a reason.. If Dogg has been struggling with something and can't take it any more which happens to me all the time, then come to Unity Answers. It's almost the same thing as 'Phone a friend' in Who Wants to be a $$anonymous$$illionaire. Someone might know, someone might not.
Do realise, you may be experienced, but some are not. Ins$$anonymous$$d of offending him, why not educate him. Be more easy on beginners, just a heads up.
Answer by v01pe_ · May 31, 2014 at 10:47 PM
You have to explicitly cast the float to an int:
restSeconds = CountSeconds = (int)guiTime;
If you want to have it rounded to the closest int use
restSeconds = CountSeconds = Mathf.RoundToInt(guiTime);
otherwise it's just truncated.