Run Coroutine once, but finish Lerping

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.

using UnityEngine;
using System.Collections;

public class Questions : MonoBehaviour {

private bool wrongAnswer = false;

public GameObject imageWrong;
public GameObject resultPos1;
public GameObject returnPos1;

void Start ()
{
    wrong.gameObject.SetActive(false);
}

void Update ()
{
    if (wrongAnswer)
    {
        StartCoroutine(LerpFalse());
    }

    if(!wrongAnswer)
    {
        StopCoroutine(LerpFalse());
    }
}

public void FoutAntwoord()
{
    wrongAnswer = true;
}

void Wrong()
{
    imageWrong.transform.position = Vector3.Lerp(imageWrong.transform.position, resultPos1.transform.position, 0.2f);
}

void WrongBack()
{
    imageWrong.transform.position = Vector3.Lerp(imageWrong.transform.position, returnPos1.transform.position, 0.2f);
}

IEnumerator LerpFalse()
{
    wrong.gameObject.SetActive(true);
    Wrong();
    yield return new WaitForSeconds(2);
    WrongBack();
    yield return new WaitForSeconds(1);
    wrong.gameObject.SetActive(false);
    wrongAnswer = false;
}

}

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.

Hope this helps.

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:

public void FoutAntwoord()  {
    wrong.gameObject.SetActive(true);
    imageWrong.transform.DoMove(resultPos1.transform.position, 0.2f)
    .OnComplete(() => {
        imageWrong.transform.DoMove(returnPosition.transform.position, 1f)
        .SetDelay(1.8f)
        .OnComplete(() => wrong.gameObject.SetActive(false));
    }); 
}

or

public void FoutAntwoord()  {
    wrong.gameObject.SetActive(true);
    DoTween.Sequence()
    .Append(imageWrong.transform.DoMove(resultPos1.transform.position, 0.2f))
    .AppendInterval(1.8f)
    .Append(imageWrong.transform.DoMove(returnPosition.transform.position, 1f))
    .AppendCallback(() => wrong.gameObject.SetActive(false));
}