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 /
avatar image
0
Question by NutellaDaddy · Mar 16, 2014 at 09:59 PM · floatcasting

Why can I make a float turn into an int?

The error says something about casting and I think it is because I can trying to multiply the variables of curHunger and curThirst by Time.deltaTime which is a float. So how can I make this work? Here's the full error: Assets/Scripts/HungerAndThirst.cs(48,49): error CS0266: Cannot implicitly convert type float' to int'. An explicit conversion exists (are you missing a cast?)

Here's the script

 using UnityEngine;
 using System.Collections;
 
 public class HungerAndThirst : MonoBehaviour 
 {
     //HUNGER VARIABLES
     public int maxHunger = 2500;
     public int curHunger = 2500;
     //THIRST VARIABLES
     public int maxThirst = 2500;
     public int curThirst = 2500;
 
     //GUI STYLES
     public GUIStyle boxes;
     public GUIStyle text;
     public GUIStyle smallerWords;
 
     void Start () 
     {
 
     }
     void Update () 
     {
         Hunger (0);
         Thirst (0);
     }
     void OnGUI()
     {
         //HUNGER AND THIRST BOX
         GUI.BeginGroup (new Rect(341,508,96,87),string.Empty, boxes);
             //HUNGER
             GUI.Label (new Rect (48, -7, 35, 120), string.Empty + curHunger, text);
             GUI.Label (new Rect (19, 14, 40, 80), "x", smallerWords);
             //THIRST
             GUI.Label (new Rect (48, 15, 35, 120), string.Empty + curThirst, text);
             GUI.Label (new Rect (19, 35, 40, 80), "x", smallerWords);
             
         GUI.EndGroup ();
 
         //AMMO AND DURABILITY BOX
         GUI.BeginGroup (new Rect(928,508,96,87),string.Empty, boxes);
         
         GUI.EndGroup ();
     }
     void Hunger(int hun)
     {
         if (PlayerMovement.isRunning == true) {
                         curHunger -= Time.deltaTime * 2;
                 } else if (PlayerMovement.isRunning == false) {
                     curHunger -= Time.deltaTime * 1;
                 }
     }
     void Thirst(int thi)
     {
         if (PlayerMovement.isRunning == true) {
             curThirst -= Time.deltaTime * 2;
         } else if (PlayerMovement.isRunning == false) {
             curThirst -= Time.deltaTime * 1;
         }
     }
 }
 
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 Eric5h5 · Mar 16, 2014 at 10:34 PM 0
Share

There's no point doing "if (somebool == true) else if (somebool == false)" since a bool can only ever be true or false...just use if/else.

2 Replies

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

Answer by getyour411 · Mar 16, 2014 at 10:02 PM

http://forum.unity3d.com/threads/27511-Converting-float-to-integer

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

Answer by Stevenwithaph · Mar 16, 2014 at 10:07 PM

The error is saying that you are trying to multiple a int by a float and casting the variable to a float would be the solution.

Just change the ints to floats and everything should work fine

 //HUNGER VARIABLES
 public float maxHunger = 2500f;
 public float curHunger = 2500f;
 //THIRST VARIABLES
 public float maxThirst = 2500f;
 public float curThirst = 2500f;
Comment
Add comment · Show 5 · 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 getyour411 · Mar 16, 2014 at 10:10 PM 0
Share

Without knowing more of the game design, that might cause more problems than it solves. GUI fields might start appearing as 25.23412421 ins$$anonymous$$f of 25; other references to these vars expecting an int might break, etc.

avatar image Eric5h5 · Mar 16, 2014 at 10:31 PM 0
Share

It clearly has to be a float, though, since the code has stuff like "curThirst -= Time.deltaTime * 2", which can't work with an int. (Well, unless the framerate was < 2fps.) You can always just display floats without the decimal part for the GUI.

avatar image Stevenwithaph · Mar 16, 2014 at 10:34 PM 0
Share

Converting Time.deltaTime to a int would just return 0 and then the values wouldn't decrease anymore.

For the GUI boxes he could simply do $$anonymous$$athf.Ceil(float) to avoid the issue with the decimal places.

I'm assu$$anonymous$$g with how the values are used here the proper type he's looking for is a float.

avatar image NutellaDaddy · Mar 16, 2014 at 11:11 PM 0
Share

I don't want the decimal point or any hundreths numbers to show. How could I do this? Could one of you helpful people give me an example. $$anonymous$$yabe that involves the $$anonymous$$athf.Ceil(float) thing.

avatar image NutellaDaddy · Mar 16, 2014 at 11:33 PM 0
Share

Never $$anonymous$$d. Thank you all ,but I ended up using $$anonymous$$athf.Ceil(float) and it works perfectly. Do any of you know where I could learn about things like $$anonymous$$athf and how to use them?

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

22 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

Related Questions

Getting an array of floats from a .bytes asset 0 Answers

Direction and Distance from One Object to Another 1 Answer

Is There A Way To Cast On 1 Returning Value 2 Times? 5 Answers

Is there a setting to change integer rounding to 'Swedish Rounding'? 2 Answers

Error Code with Unity just sitting with a project open, what is it, how to fix? 4 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