I’m trying to make an image come up when I press a UI.Button, and go back down after 2 seconds, but at the moment as soon as I press the button, it will run everything in the coroutine about 180 times (obviously because it’s in the Update()), but when I put an isLerping bool check on the if (wrongAnswer), it will run everything once, like it should, but the Lerp won’t work. What it’ll do is, the image pops up about 1 frame so you only see the very top of the image, then after 2 seconds, pops right back down, not in a smooth transition way, but in a snapping way. I feel like I’m making a very obvious and dumb mistake here but I honestly can’t wrap my mind around it, help would be greatly appreciated.
Edit: The UI.Button calls FoutAntwoord() when clicked, I forgot to add that to the above.
I think the problem here is that you are starting the coroutine every update whilst wrongAnswer is true, but wrong answer is only set to false at the END of the coroutine, some 3 seconds later. Meaning you are starting many coroutines each one calling Wrong() at least once before waiting.
I suggest a couple of solutions here. The first, is to have a way of knowing if you coroutine is running, and only calling StartCoroutine if it isn’t already running. You can either do this via a boolean flag that you can set at the start of the coroutine, and reset at the end, or could actually store the Coroutine object returned by StartCoroutine and use that instead of the booelan flag (but still having to null that value at the end of the coroutine).
Finally you could also (would suggest doing this in addition to the above) Rework your code so that the detection of a wrong answer can trigger a single explicit function call instead of just setting a flag, since then you only have one place to trigger the coroutine and don’t have to worry about it potentially happening in each update loop at all. since the wrong answer event should only occur periodically instead of every frame, so the code could be structured to reflect that as well.
You can use DoTween (free in asset store) to do program animations (move, fade, etc). It’s awesome and simple to use.
They add extension methods for many Unity components (you need using DG.Tweening; to use it) and your code will be like: