Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Dogg · May 31, 2014 at 10:35 PM · error

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dogg · May 31, 2014 at 10:57 PM 3
Share

"$$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.

avatar image Lovrenc · May 31, 2014 at 11:16 PM 0
Share

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.

avatar image meat5000 ♦ · May 31, 2014 at 11:27 PM 0
Share

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!

avatar image DimitriUK · Jul 10, 2015 at 03:07 PM 6
Share

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.

avatar image
3

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Script is not working at all 2 Answers

How do I get rid of these scripting Errors? 2 Answers

No appropriate version of 'UnityEngine.AudioSource.Play' for the argument list '(System.Type)' was found. 1 Answer

Help Solve This 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges