- Home /
IEnumerator not continuing after yield return
So I'm trying to get my IEnumerator method to work properly, I've used IEnumerators before and they've worked perfectly. However for some reason I can't get this specific one to work.
//Insert a new subject.
public IEnumerator InsertSubject(string subjectName, int subjectImage, Action Success, Action Fail)
{
WWWForm addSubjectForm = new WWWForm();
addSubjectForm.AddField("functionSwitchPost", "InsertSubject");
addSubjectForm.AddField("subjectNamePost", subjectName);
addSubjectForm.AddField("subjectImagePost", subjectImage);
WWW databaseConnection = new WWW(subjectsSchoolUrl, addSubjectForm);
Debug.Log("Before");
yield return databaseConnection;
Debug.Log("After");
if (databaseConnection.error == null)
{
Debug.Log("Win");
Success();
}
else
{
Debug.Log("Fail");
OnNoConnection(Fail);
}
}
Debug.Log("Before") executes but Debug.Log("After") doest not and anything beyond yield return databaseConnection. It works in other IEnumerators, where it will execute after the first yield return but not this particular method. Is it because of my parameters? That's the only thing different between them. If it is because of the parameters does anybody know why?
Answer by Demonolith · Jun 05, 2018 at 11:27 PM
I figured it out, when calling the coroutine from another script I had to specify the class it came from. so I did databaseSubjectsScript.StartCoroutine(databaseSubjectsScript.InsertSubject(subjectNameInputField.text, selectedSubjectImage, OnAddSubjectSuccess, AddNewSubject));
Note that it's perfectly possible to run the coroutine on a different script. However if the object you used to run the coroutine on is destroyed or deactivated all coroutines on that object will be stopped. We don't know from where you actually start your coroutine, but you probably deactivate or destroy the object / script where you started the coroutine from.
It doesn't matter where the IEnumerator method is located. If can be even a static method. The object where you call StartCoroutine on will actually own the coroutine that is created.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# PHP returns a blank message 0 Answers