Co-routine not completing

Hi, I need a co-routine to run, and some more code to run after it finishes. I’ve looked at a number of examples, and yet none seem to work.

This is the code I have so far. All the logs are displayed, except ‘print(“2”)’, which is where I want the rest of my code to run.

protected override void Pick()
        {
            Debug.Log("Scale...");
            StartCoroutine(Example());
            Debug.Log("End");
        }
           
        IEnumerator Example()
        {
            print("1");
            float ResumeTime = Time.realtimeSinceStartup + 5f;
            while (Time.realtimeSinceStartup < ResumeTime)
            {
                yield return null;
            }
            print("2");
        }

Any ideas?

if you just want to wait for x amount of time, remove the for loop and
yield return new WaitForSeconds(5);

I took you code and tested it the best way I could, and it works for me. I did remove the protected override. and added a small debug.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class coroutinetest : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Pick();

    }
   

    void Pick()
    {
        Debug.Log("Scale...");
        StartCoroutine(Example());
        Debug.Log("End");
    }

    IEnumerator Example()
    {
        print("1");
        float ResumeTime = Time.realtimeSinceStartup + 5f;
        Debug.Log(Time.realtimeSinceStartup + " : " + Time.realtimeSinceStartup + 5f);
        while (Time.realtimeSinceStartup < ResumeTime)
        {
            yield return null;
        }
        print("2");
    }
}

3273703--253049--upload_2017-11-1_19-31-14.png

It worked for me. I used a start instead of the pick function.

Code looks fine.

Do you - by any chance - deactivate or destroy the object that this script is attached to? If so, that would cancel/stop the coroutine.