Hey everyone, I’m having an issue with this coroutine I need to run. It keeps spitting out an error saying that it can’t convert WaitUntil to string, and when I add WaitUntil to the return types, it spits another saying it can’t be returned. I’ve tried researching for a few hours now to no avail. Here’s the snippit of code:
public string OpenSaveDialog(string Title, string OpenLocation, string[] AllowedExtentions)
{
OpenBtn.GetComponentInChildren<TMPro.TMP_Text>().text = "Save";
TitleText.text = Title;
AllowFileNameTyping = true;
MultiSelect = false;
LoadIntoExplorer(PathInput, AllowedExtentions);
return StartCoroutine(WaitForFinish()); // Error here: Cannot implicitly convert type 'UnityEngine.Coroutine' to 'string'
}
IEnumerator<string> WaitForFinish()
{
yield return new WaitUntil(() => Done == true); // Error here: Cannot implicitly convert type 'UnityEngine.WaitUntil' to 'string'
yield return FilePathsSelected[0];
}
So coroutines aren’t actually asynchronous methods, even though they are used as such by Unity. They are enumerators. They provide an enumeration of things. Crucially, they must enumerate items that are all of the same type. In the case of coroutines, they return some wait for some time period to pass
object. This won’t allow you to directly return a string.
What you can do, however, is track and set what is known as an Option
, Maybe
, or Result
object.
You could do something like:
public class CoroutineResult<TResult> {
public bool Completed { get; private set; }
public TResult Result { get; private set; }
private CoroutineResult<TResult> (); // Factory construction only
public static CoroutineResult<TResult> Complete (TResult result) {
return new CoroutineResult<TResult> { Completed = true, Result = result };
}
public static CoroutineResult<TResult> NotComplete() {
return new CoroutineResult<TResult> { Completed = false }
}
}
public class Client : MonoBehaviour {
private CoroutineResult<string> _result = CoroutineResult<string>.NotComplete();
public void Update () {
if (_result.Completed) {
DoSomething (_result.Result);
}
}
public IEnumerator WaitForFinish () {
// Not real clear on what you're doing here but I'll assume it works
yield return new WaitUntil(() => Done == true);
_result = CoroutineResult<string>.Complete ("MyResult");
}
}