- Home /
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;
}
}
}
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.
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;
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.
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.
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.
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.
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
Follow this Question
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