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?
johne5
2
if you just want to wait for x amount of time, remove the for loop and
yield return new WaitForSeconds(5);
johne5
3
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");
}
}

It worked for me. I used a start instead of the pick function.
Suddoha
5
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.