Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by LeftyTwoGuns · Sep 23, 2015 at 09:14 PM · c#floatintconvert

Cannot convert float to int?

I have a mana system and a specific attack will drain this as the mouse button is held down. The lowest float I set it to (1) still drains it too fast. But I can't drop below that because I get the "cannot implicitly convert type" error. This is from the mana system script:

 void Update()
     {
         manaValue.value = Mathf.Lerp(manaValue.value, (float)player.playerMana / (float)player.playerMaxMana, 0.5f);
     }

And this is a separate script where I'm draining the mana and trying to use an int:

 if (Input.GetMouseButton(0))
             {
                controller.playerMana = controller.playerMana - 0.5f;
             }


What can I do to fix this?

Comment
Add comment · Show 1
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 Jessespike · Sep 23, 2015 at 09:21 PM 1
Share

Try using floats for player$$anonymous$$ana and player$$anonymous$$ax$$anonymous$$ana?

1 Reply

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

Answer by Cherno · Sep 23, 2015 at 09:23 PM

Mathf.Lerp returns a float so it's not wonder you get this error.

 manaValue.value = (int)Mathf.Lerp(manaValue.value, (float)player.playerMana / (float)player.playerMaxMana, 0.5f);

or

    manaValue.value = Mathf.RoundToInt(Mathf.Lerp(manaValue.value, (float)player.playerMana / (float)player.playerMaxMana, 0.5f));

However, I don't know if the lerping would actually change the manaValue when rounding the end result. A better way would be to make the manaValue a float and display it with Mathf.RoundToInt if you don't want decimals in your GUI. Generally speaking, if lerping and similar operations are involved, it's best to make the variable a float and not an int. If you really want to keep it as an int, you can also consider slowing the rate at which it decreases, for example with a CoRoutine that waits a certain amount of time between decreasing the amount by 1.

 IEnumerator DecreaseMana() {
      while(true) {
           manaValue.value --;
           yield return new WaitForSeconds(1f);
      }
 }

Comment
Add comment · Show 1 · 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 LeftyTwoGuns · Sep 23, 2015 at 09:36 PM 0
Share

Ok, so I did decide to just change the mana values to floats. This was my misunderstanding because now that I know a little more about it, I definitely do not want them as strictly ints. So thank you both for helping me understand better!

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

28 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 avatar image avatar image avatar image avatar image

Related Questions

Convert "Double" to "Float" or "Int"? 1 Answer

Subtracting a time difference from an int? 1 Answer

Color float rgb values cannot be converted correctly to int, and hex. 1 Answer

How to convert float to long 4 Answers

Convert Float to Int for Google leaderboard 1 Answer


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