- Home /
Make a wait function
Hello, so I am trying to automate my system a little bit, basically I am trying to make it so I declare everything that I want to happen line after line, and then my code saves all this into a List and runs them one after the other. Now that's good and all, but I have a small problem, when I want the game to just wait a certain amount of time (because I want cinematics to play while this wait function plays or something else).
So I am using coroutines of course, and after running my wait function, nothing happens, it doesn't run the next line, so please help?
Thanks!
here is my code:
public bool isDone = true;
List<string> methodList = new List<string>();
void Start ()
{
AddNext("test", new string[] { "hey" });
AddNext("wait", new string[] { "10" });
AddNext("test", new string[] {"hey"});
}
void AddNext (string method, string[] args)
{
string temp = method + "_" + string.Join("_", args); ;
methodList.Add(temp);
StartCoroutine(AddNextNow());
}
IEnumerator AddNextNow()
{
string methodNow = methodList[0];
if (isDone)
{
isDone = false;
string[] all = methodList[0].Split('_');
if (all[0] == "test")
{
Test(all[1]);
}
else if (all[0] == "wait")
{
yield return StartCoroutine_Auto(Wait(float.Parse(all[1])));
}
methodList.Remove(methodList[0]);
isDone = true;
if (methodList[0] != null) {
StartCoroutine(AddNextNow());
}
}
}
void Test (string input)
{
Debug.Log("Test! " + input);
}
IEnumerator Wait (float delay)
{
Debug.Log("Waiting...");
yield return new WaitForSeconds(delay);
Debug.Log(methodList[0]);
}
in general just use Invoke
for timers in Unity, Is there a chance you could google for duplicates of this question first - you'll find many good answers here!
Answer by RoofTurkey · Jan 18, 2016 at 10:16 AM
Why wouldn't you just use
yield return new WaitForSeconds(delay);
Instead of an entire function for this?
As far as I know a coroutine doesn't wait before it's finished, so it won't hold back other code.
Edit: Perhaps it's worthwhile to read this: http://answers.unity3d.com/questions/276055/question-about-coroutines-and-waiting-for-a-functi.html
When a coroutine is started within another, using yield return StartCoroutine
, the outer CR does "hold back other code" (wait for the inner one to finish). That's standard.
Wrapping the WaitForSeconds call in another CR does look superfluous here, especially given that it's only used in one place (if it were used in multiple places then the wrapping function would be perfor$$anonymous$$g a potentially useful task in that it enables the logging code to be added without repetition). But that doesn't look like the problem to me.
I'm not sure what the problem is, $$anonymous$$d. The code confuses me, especially the way that 3 versions of the AddNextNow CR are started in the same frame (the Start function), and they share the isDone flag, and also each kick off the CR again at the end.
I suspect that AddNext() should not be starting the CR at all. Ins$$anonymous$$d you'd start it at the end of the Start function, once methodList has been populated - that way you'd only have one AddNextNow running at a time. But I'd probably just start over, using a flowchart.