- Home /
if(float == Time.time) not working, why?
I assume that maybe Unity is skipping over a few numbers and is missing the number that it is supposed to match. I have noticed that Time.time has like 5 numbers after the period so that could explain why. However I will ask first before trying to work around the problem. I was also think about maybe using clamp to say if(float == (any number inside the clamp)) but this probably wont work. I have other solutions that WILL work but I want to try using == Time.time first. Here's some code to help you understand
var hpRayStall : float;
Update(){
if(Input.GetButtonDown("P2hp"))
{
hpRayStall = Time.time + 2;
}
if(hpRayStall == Time.time)
{
var hpRay = Physics.Raycast(lp.transform.position, transform.TransformDirection(Vector3.right), 2);
}
if(hpRay)
{
sceneManager.GetComponent(SceneManagerScript).p1HP -= 6;
}
You should genereally never == two floats/doubles, especially one like Time.time. The comparison is not 100% precise. Better check with x = y
Answer by highpockets · Mar 09, 2014 at 01:02 AM
I think you may find that a coroutine will work better. I'm thinking that maybe your problem is due to the fact that Time.time might not ever be exactly the number that you are looking for because of the small gap between each update. If you put this in a coroutine, you can guarantee the execution of the code.
Yea that's what I was thinking too. I figure it's jumping over the exact number. Usually I see people saying to avoid coroutines. Honestly I'm not sure what coroutine means. I assume it's yield WaitForSeconds.
avoid coroutines!! Who said that???
Avoid the Update function, because it processes every frame which is not very optimized if you don't need it. I don't think I have a single line of code in the update function because of that. Coroutines are great. I think, maybe the only thing to worry about when you use a coroutine is to make sure that you don't start multiple instances and you don't forget to stop it when you don't need it anymore if you have a while loop in it.
Anyways:
bool inRoutine = false;
function Update()
{
if(Input.GetButtonDown("P2hp"))
{
if( !inRoutine )
{
inRoutine = true;
StartCoroutine( RayStall() );
}
}
}
function RayStall()
{
yield WaitForSeconds( 2 );
//your ray code...
inRoutine = false;
}
I also put in a bool to ensure that you don't start multiple coroutines.
Ok just so were in agreement, Unity is probably skipping over the exact number correct. It was the only reason I could think of and you came up with the same conclusion so I guess it's unanimous.
It's very likely that that is the case. I wouldn't put any kind of blame on unity, Time.time is a float and update runs every frame, lets say you have 30 fps, 'on average', that means Time.time will be accessible 60 times in the update function over that 2 seconds that you are waiting for the results. There is certainly no guarantee that the 60th time will be exactly 2 seconds (it might be 2.001 or 1.9999), as frame rates are constantly fluctuating. You probably miss it by a millisecond or 2 every time. The chances you hit it right on the money are probably less than your chances at winning a scratch and win, though I've never had any luck with the lottery!!
just an idea. You could try to check if float >= Time.time-Time.deltaTime && float
Your answer
![](https://koobas.hobune.stream/wayback/20220613135742im_/https://answers.unity.com/themes/thub/images/avi.jpg)