Hello, everyone.
All I want to do is wait for 0.1 second before setting a boolean to true.
Is there a simple way to do it without using coroutine or invoke?
Thanks in advance.
Hello, everyone.
All I want to do is wait for 0.1 second before setting a boolean to true.
Is there a simple way to do it without using coroutine or invoke?
Thanks in advance.
I believe coroutines are the better way to do these kind of things as it allows code to keep on running, without freezing the game. Also, they can be used outside of the Update block
But there is workarounds, one of them imply using a timer in the update function with a bit of boolean values like so :
float timer = 0;
float timeToWait = 0.1f;
bool checkingTime;
bool timerDone;
void Update()
{
if (checkingTime)
{
timer += Time.deltaTime;
if (timer >= timeToWait)
{
timerDone = true;
checkingTime = false;
timer = 0;
}
}
if (timerDone)
{
//DoSomething()
timerDone = false;
}
}
The only thing left is to set CheckingTime to true somewhere
well, Personally, i'd use coroutines, but the OP probably has his reasons. I actually didn't think of the Invoke, that's another good way to go about it. Much shorter that doing that whole update Timer
– jokimSorry @jokim - had intended this to be a comment under the question to the OP, not a comment on your answer!
– tanoshimi@tanoshimi I use Invoke currently, but my inner perfectionist doesn't want to create a dedicated function for one line of code. And I don't need all power of a coroutine for one simple thing.
– 5pointsevenI know this is an old post, but I have a nice solution and thought I’d share it. An added bonus is that no Windows interrupt rate artifacts will be encountered:
public static void Wait(long milliseconds)
{
var sw = Stopwatch.StartNew();
var ticks = (milliseconds * Stopwatch.Frequency) / 1000;
while (sw.ElapsedTicks < ticks) { }
}
Solved my issue: delaying program execution without the need to split up the code into coroutine(s). Used System.Diagnostics.Stopwatch, as otherwise I get a conflict with Debug which is also defined in Unity.
Threading and a delegate.
– Landernpublic float timer; public bool count =true; void Update() { if(count){ if(timer >=0.1) { yourbool=true; } } }
– Jeric-MianaAnother important thing Boolean is set outside the update function. It's set when 2 fingers are lifted off the screen to prevent another script to registering a nanosecond while one of fingers is still there as a single touch.
– 5pointseven