- Home /
C# simple delay execution without coroutine?
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.
public float timer;
public bool count =true;
void Update()
{
if(count){
if(timer >=0.1)
{
yourbool=true;
}
}
}
Another 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.
Answer by jokim · Sep 09, 2014 at 01:39 PM
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
Why don't you want to use Coroutines or Invoke with a delay?
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. $$anonymous$$uch shorter that doing that whole update Timer
Invoke is shorter if you're not going to use parameters for whatever function you want to execute That's why I stopped using it
Sorry @jokim - had intended this to be a comment under the question to the OP, not a comment on your answer!
@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.
Answer by rich2020 · Jan 24, 2017 at 11:23 PM
I 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.
This makes no sense. Blocking the whole application is something you never want to do. During that time no visual update happens and if it takes too long the OS might flag your application as "not responding" and might even kill your application.
Apart from that just blocking for a certain amount of time is done by using
System.Threading.Thread.Sleep(milliseconds);
Which does blocking the current thread for the specified amount of time. Using Sleep doesn't eat up any CPU performance unlike a spinning while loop.
@Bunny83: In my case it does work (turn based game), but I agree that Sleep
is the better way to do this especially on mobile saving power. So I have switched to the Sleep
implementation. Thanks.