Logic problem looping & timing

I'm having trouble calling a function at the correct time while looping. Either it calls it too fast or too slow. I thought I figured it out yesterday, but I didn't... Here is a diagram to show what I'm trying to do, I know I have some logic issue going on.

In Update() I call a function called blinkAtRandom(); This function has a timer that goes off X amount of time. When this timer goes off, it calls BlinkOnce();.

BlinkOnce() will change the texture for the eye animation, but only 1 texture per Y amount of time, so I want to loop through this eyelidTextures.Length amount of loops

The problem now, is looping ignores the timers so my results are either too "fast" (blink never can become false) or too "slow" (only changes 1 texture through an entire loop, and blink is always true).

Here is a sample of my code. I also want to point out if you stick BlinkOnce() by itself in update, it changes at the correct time and looks like Ideal Timing, so it works, it just can't be called from another function or I get the timing issues.

I figured out the problem was, I needed to have 1 coroutine with a bunch of yields in it as opposed to 3 with each their own yield for x seconds. That was what was giving me the timing trouble. Anyway, got my code working, thanks!

Well, I have made them all into Coroutines but I'm still having trouble. FixedUpdate and LateUpdate won't fix a logic issue... :/

It flashes like mad now, but it's starting to sorta work, kinda, it flashes at the right time for one of my coroutines so I think I'm getting close, now if only I could get them to match up correctly and slowdown a bit... (Okay, I realized the timing was better because I used the function version not the coroutine version, so I think I'll have to revert back to functions...) Update vs LateUpdate vs FixedUpdate really no noticeable difference in my scene btw. (Also since I'm using time.deltaTime it should be comparable to FixedUpdate...)

Hmmm... After more testing ~ coroutines are super fast so if there were some way to slow them down by like 90% I would be maybe okay... My blinkrange is 5-7 seconds, my resetTime is set to 15 seconds, timer2 set to 30sec... it just seems to be flickering.... I don't know coroutines seem to be a lot harder to control then regular functions... :/

The problem with your code right now is that you are starting a coroutine every time FixedUpdate (or Update) is called.

This means you're starting a coroutine every frame (in the case of Update), or every physics time step (in the case of FixedUpdate), which defeats the purpose of waiting for a certain amount of time. (I.e. the first coroutine will wait for some time, but during that time, another coroutine will run and make your image flash.)

I have provided a solution that avoids this problem by starting the coroutine inside Start, so that only one coroutine is running.

Well, I've moved it to start, it will probably work after I fiddle with it so more. Coroutines keep repeating, until I tell them to stop correct? It should work at some point so I'll go with your answer and give you the credit.

Your answer

Hint: You can notify a user about this post by typing @username

Attachments:
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.