Whenever this is run, the only line printed to the console is Start, which says to be it isn’t even running the Wait coroutine (and thus, StartCoroutine isn’t working), but why?
I’d expect “Wait1” would be printed immediately before “Start”. So is “Wait1” not even being printed at all, including before “Start”?
Then, “Wait2” would be printed later, depending on the implementation of WaitFor.Frames(frames). Is WaitFor your own utility class, or one that you acquired somewhere?
Ah, yes. Wait1 WAS printed before Start, I have a few other scripts with Debugs in, must have missed it, but definitely nothing after that, though.
The WaitFor class looks like this:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class WaitFor
{
public static IEnumerator Frames(int frames)
{
if (frames <= 0)
{
throw new ArgumentOutOfRangeException("frameCount", "Cannot wait for less that 1 frame");
}
while (frames > 0)
{
frames--;
yield return null;
}
}
public static IEnumerator Seconds(int seconds)
{
yield return new WaitForSeconds(seconds);
}
}
Not terribly complex, but a useful thing to keep isolated and out of local classes as I can access it anywhere.
From my script in total, I should get Wait2 after 5 frames, after my input stuff has had time to initiate (that’s what this whole thing is designed to do, prevent errors with things missing before the initialisation of my input setup during the first few frames) but it never appears.
I should also receive (SHOULD) “Function” in the console, but obviously, it isn’t getting there either.
I am at a loss to explain this, the code I just posted shouldn’t prevent anything, it should just wait for 5 frames from what I can see.
Something else must be going on in your code. I added this script to my project
WaitFor Class
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class WaitFor
{
public static IEnumerator Frames(int frames)
{
if (frames <= 0)
{
throw new ArgumentOutOfRangeException("frameCount", "Cannot wait for less that 1 frame");
}
while (frames > 0)
{
frames--;
Debug.Log("Frame CountDown..." + frames);
yield return null;
}
}
public static IEnumerator Seconds(int seconds)
{
yield return new WaitForSeconds(seconds);
}
}
And this script to implement what your were doing:
Main Script
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.AccessControl;
public class GenericTest : MonoBehaviour
{
void Start()
{
StartCoroutine(Wait(5));
Debug.Log("Start");
}
public IEnumerator Wait(int frames)
{
Debug.Log("Wait1");
yield return StartCoroutine(WaitFor.Frames(frames));
Debug.Log("Generate Controls");
Debug.Log("Wait2");
}
}
And got the following debugs as expected:
Wait1
Frame CountDown … 4
Start
Frame CountDown … 3
Frame CountDown … 2
Frame CountDown … 1
Frame CountDown … 0
Generate Controls
Wait2