- Home /
Int, Colliders, and GUI
So here's my script i gonna add some feelings and other stuff that the player expresses. So here i want my "warm" int to be higher every secound IF im not in one of the shadows colliders. Then if i am inside of one, the "warm" int is going to be get lower. It's also displayed on the screen. But here's the problem, the "warm" int never change. And i even set it to 50 in the begining. Help please! ~carlqwe
Code: #pragma strict
var warm : int = 50;
function Update ()
{
}
function OnTriggerStay (Col : Collider)
{
if(!Col.tag == "shadows")
{
warm += 1 * Time.deltaTime;
}
else
{
warm -= 2 * Time.deltaTime;
}
}
function OnGUI ()
{
GUI.Label(new Rect(780, 680, 250, 100), warm.ToString(""));
}
Answer by Baste · May 16, 2015 at 09:56 PM
Warm is an integer. You're adding 1 * Time.deltaTime to it - which is a float. That float will be rounded to an integer before it's added to the warm variable. In all of the languages used in Unity, rounding is always towards zero.
So, your 1 * Time.deltaTime is somewhere in the realm of 0.01 or lower, so you're actually never adding (or subtracting) anything from the warm variable. The easiest solution is to just change the warm variable to a float. When you're printing it, you should probably cast it back to an integer, so it doesn't print all of the decimal places.
Okay thank you, but if u where to change my script so it worked how whould you do it?
As he said, just change the int to float like this:
var warm : float = 50;
Answer by Multifred · May 17, 2015 at 03:10 PM
the reason it never change is that your field warm is an integer: 1*Time.deltaTime is lower than 1 so it's as if you added 0
change warm into a float and create a GetWarm function that return (int)warm;
Your answer
Follow this Question
Related Questions
iTween colliders 1 Answer
GUI Text to GUI Label Script? 1 Answer
Counter with a Rounded GUI bar 1 Answer
Arrange ints from biggest to smallest and display them in a table 1 Answer
i cant make a label show my var Health : int = 100; 1 Answer