• 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#floatconvertint

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Is there any way to subtract int / float if another int / float subtracted? 1 Answer

Subtracting a time difference from an int? 1 Answer

How to convert float to long 4 Answers

Int not getting rounded data 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges