- Home /
Disable an if statement in update
If I want to do something for the first few frames of Update(), but I don't want my script to have to continue checking that condition for the rest of the game, is there a common way to disable that statement in Update?
I know it isn't a huge performance cost, but it seems inefficient to check a condition for the first 5 or 10 frames and then have to continue checking that for the thousands of frames afterwards when I no longer need it.
Answer by tormentoarmagedoom · Jan 30, 2019 at 08:59 PM
Good day.
You can always make a script that do what you want, that destroys himself after x time or frames or when you want.
Bye!
Answer by James_BadAlchemy · Jan 30, 2019 at 08:58 PM
There are better ways to accomplish this most of the time. You're correct, it's pretty weird to have un-needed if checks in update if they're never going to be used again. If it's possible, I'd look into InvokeRepeating if you can. Syntax is: InvokeRepeating("MethodName" string, timeBeforeStart float, repeatIntervalTime float).
You can stop these Invocations at anything with CancelInvoke("MethodName" string);
Answer by Tasarran · Jan 31, 2019 at 07:25 PM
InvokeRepeating is one way, but I'd look into using a CoRoutine to do this. Having that tool in your toolbelt will be generally useful in wider applications.