Unable to lerp in coroutine

Hi all.

I made an script so that menu items do appear smoothly. I’m able to make the code run within update but unable to make it run within a coroutine and I’m unable to find the issue. Anyone can see what is wrong?

using UnityEngine;
using System.Collections;

public class MenuLerp : MonoBehaviour {

    GameObject objToMove;
    float delay;

    public float lerpTime = 1f;
    float currentLerpTime;

    public float moveDistance = 10f;

    Vector3 startPos;
    Vector3 endPos;

    public bool inwards;
    public bool outwards;

    public enum ComingFrom{up, down, right, left};
    public ComingFrom direction;
    /*
    public bool up;
    public bool down;
    public bool right;
    public bool left;
    */

    // Use this for initialization
    void Start () {
   
        if (inwards == true){
            endPos = transform.position;

            switch (direction){
                case ComingFrom.up:
                startPos = transform.position + transform.up * moveDistance;

                break;
            case ComingFrom.down:
                startPos = transform.position + -transform.up * moveDistance;

                break;
            case ComingFrom.left:
                startPos = transform.position + transform.right * moveDistance;

                break;
            case ComingFrom.right:
                startPos = transform.position + -transform.right * moveDistance;

                break;
            }

            //StartCoroutine (test());

           
            //startPos = transform.position + transform.up * moveDistance;

            //EL OUTWARDS MIENTRAS EL LERP ESTE EN UPDATE, PUES NADA.
        }
        if (outwards == true){
            startPos = transform.position;
            endPos = transform.position + transform.up * moveDistance;

        }


    }
    /*
    protected void Start() {
        startPos = transform.position;
        endPos = transform.position + transform.up * moveDistance;
    }
    */


    protected void Update() {
        //reset when we press spacebar
        if (Input.GetKeyDown(KeyCode.Space)) {
            currentLerpTime = 0f;
        }
       
        //increment timer once per frame
        currentLerpTime += Time.deltaTime;
        if (currentLerpTime > lerpTime) {
            currentLerpTime = lerpTime;
        }
       
        //lerp! // ease In
        if (inwards==true){
            float t = currentLerpTime / lerpTime;
            t = Mathf.Sin(t * Mathf.PI * 0.5f);
            transform.position = Vector3.Lerp(startPos, endPos, t);
        }

        //lerp! // ease out
        if (outwards==true){
            float t = currentLerpTime / lerpTime;
            t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
            transform.position = Vector3.Lerp(startPos, endPos, t);
        }
    }


/*
    IEnumerator test(){

        currentLerpTime = 0f;
        lerpTime = 40f;
        float t = 0f;

        while ( t <lerpTime)
        {
            t = currentLerpTime / lerpTime;
            currentLerpTime += Time.deltaTime;
            //t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);

            transform.position = Vector3.Lerp( new Vector3(0,500,0), new Vector3(0,0,0), t);

           
            Debug.Log ("t" + t);

        }
        yield return new WaitForEndOfFrame();

You need to yield inside the while loop inside the test coroutine. You are basically doing the entire while loop inside the coroutine, then yielding once.

Running to check this! I’ll tell how it goes!

Quick addition: “yield return null;” waits for one frame.

Thank you Korno! and you too StarManta!!

here I leave the script for anyone interested, great for menu items ease in / out

using UnityEngine;
using System.Collections;

public class MenuLerp : MonoBehaviour {

    public float delay;

    public float lerpTime = 1f;
    float currentLerpTime;

    public float moveDistance = 10f;

    Vector3 startPos;
    Vector3 endPos;

    public bool autostart;
    public bool inwards;
    public bool outwards;

    public enum ComingFrom{up, down, right, left};
    public ComingFrom direction;


    void Start () {

            endPos = transform.position;

            switch (direction){
                case ComingFrom.up:
                startPos = transform.position + transform.up * moveDistance;

                break;
            case ComingFrom.down:
                startPos = transform.position + -transform.up * moveDistance;

                break;
            case ComingFrom.left:
                startPos = transform.position + transform.right * moveDistance;

                break;
            case ComingFrom.right:
                startPos = transform.position + -transform.right * moveDistance;

                break;
            }

        if(autostart){
            StartCoroutine (IEase());
        }
    }



    protected void Update() {
        /*
        //reset when we press spacebar
        if (Input.GetKeyDown(KeyCode.Space)) {
            currentLerpTime = 0f;
        }
      
        //increment timer once per frame
        currentLerpTime += Time.deltaTime;
        if (currentLerpTime > lerpTime) {
            currentLerpTime = lerpTime;
        }
      
        //lerp! // ease In
        if (inwards==true){
            float t = currentLerpTime / lerpTime;
            t = Mathf.Sin(t * Mathf.PI * 0.5f);
            transform.position = Vector3.Lerp(startPos, endPos, t);
        }

        //lerp! // ease out
        if (outwards==true){
            float t = currentLerpTime / lerpTime;
            t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
            transform.position = Vector3.Lerp(startPos, endPos, t);
        }
        */
    }

    public void Ease(){
        StartCoroutine (IEase());

    }

    IEnumerator IEase(){

        currentLerpTime = 0f;
        lerpTime = 1f;
        float t = 0f;

        yield return new WaitForSeconds(delay);

            while ( t <lerpTime)
            {
                currentLerpTime += Time.deltaTime;
                if (currentLerpTime > lerpTime) {
                    currentLerpTime = lerpTime;
                }

                t = currentLerpTime / lerpTime;

                if (outwards==true){
                    t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
                    transform.position = Vector3.Lerp( endPos,startPos , t);

                }
                else{
                    t = Mathf.Sin(t * Mathf.PI * 0.5f);
                    transform.position = Vector3.Lerp( startPos, endPos, t);

                }
                Debug.Log ("t" + t);
                yield return null;