- Home /
If "equals" doesnt work.
In this script you can see how time goes up from 0 (using Time.deltaTime). In my case,i want that unity prints massage for me when time is exactly 35 (FirstPass) Function works if time is greater than or less than Firstpass,or second or third. But when i want that time is exactly equals to FirstPass,which is 35...the massage is not showing... By the way it works only when i disable time counting up and enter the value manually.... Why? Please.
#pragma strict
var time : float = 0.0f;
var FirstPass : float = 35.0f;
var SecondPass : float = 45.0f;
var ThirdPass : float = 55.0f;
var SpeedOfTime : float;
function Update ()
{
//if(Input.GetKeyDown(KeyCode.Space))
TimeStarts();
time += Time.deltaTime * SpeedOfTime;
}
function TimeStarts ()
{
if(time == FirstPass)
{
print("FirstPass");
}
if(time > SecondPass)
{
print("SecondPass");
}
else if (time == ThirdPass)
{
print("ThirdPass");
}
}
Answer by tanoshimi · Apr 04, 2015 at 03:00 PM
Because there's only a very small chance of that line of code being executed in a frame after the game has been running for exactly 35 seconds...
Thumbs up for everyone! Thanks for help guys. Is there any other way to approach this? Well if i do "greater than" 35,how can i make that massage print Only Once,not continuously?
While I'm sure there's probably a better way to do this overall, I would probably use bools :)
public bool FirstPassDone;
public bool SecondPassDone;
public bool ThirdPassDone;
function TimeStarts ()
{
if(time > FirstPass)
{
if (FirstPassDone == false)
FirstPassDone = true;
print("FirstPass");
}
}
if(time > SecondPass)
{
if (SecondPassDone == false)
SecondPassDone = true;
print("SecondPass");
}
}
else if (time > ThirdPass)
{
if (ThirdPassDone == false)
ThirdPassDone = true;
print("ThirdPass");
}
}
}
Sam
Hello again $$anonymous$$ark, if you look at the answer that i've written its possible to do it by using equals, no need for bigger or smaller comparasion. Do you need me to write the code?
Hey,thank you guys again,for everything. I figured out,that the best way would be using booleans,it works very well,thanks Sam for that. And ozturkcompany,i really have no idea how to use $$anonymous$$athf. functions :D But,thank you anyway.
Answer by _joe_ · Apr 04, 2015 at 03:01 PM
You're testing in an Update, The condition will basically never work, because it's changing so fast odds are it's going to skip the exact value.
In the Update it's always best to test for =, that way you can be sure that the value passed, or better, find a different approach for your tests.
Cheers (y)
I think a greater-than sign fell off (the = is really a "greaterThan=". HT$$anonymous$$L stripping does that. I think marking as code will work, test: >=
. And an html-style test: >= .
Answer by ozturkcompany · Apr 04, 2015 at 03:18 PM
Most of the time it won't work because the value of time won't be the exact value that you defined in your variables. It is mostly likely that you could get a result like this; time = 35.00000123 and your FirstPass = 35.00000000 which these two values don't match. What you can do is comparing these two values with the following, either you comparing using Mathf.Approximate or just Round your time value using Mathf.Round and it will be all fine then.
Your answer
Follow this Question
Related Questions
Total Time Played since Installation 2 Answers
countdown timer acivation 1 Answer
Accumulating deltaTime oddity 1 Answer
Accurate timing while using timeScale. 1 Answer
I cant get this timer to start when this bool turns true? 1 Answer