- Home /
Question by
Luiz_Thiago · Dec 05, 2014 at 08:40 PM ·
coroutinecoroutine errors
Coroutine resetting a FOR iterator.
Hello everybody,
I have a question. I am performing a repeat loop with a FOR. In this "FOR", I'm calling a "YIELD RETURN NULL" in each repetition of the loop. The problem is that my iterator is always 0, and the loop eventually becomes infinite!
I recorded a video with the problem: VIDEO
And the code:
IEnumerator ProcessSaveGame () {
_isSaving = true;
Data = new PlayerData();
var buildings = StructuresManager.GetBuildingList();
if (buildings != null && buildings.Count > 0)
{
yield return StartCoroutine(SaveBuildings(buildings));
}
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/gameData001.dat");
bf.Serialize(file, Data);
file.Close();
_isSaving = false;
}
IEnumerator SaveBuildings (List<Building> buildings) {
for (int i = 0; i < buildings.Count; i++)
{
AddBuildingData(buildings[i]);
yield return null;
}
}
Comment
what do you get by adding a print(buildings.Count);
at the start of SaveBuildings()
?
EDIT: any reason why you made it a coroutine?
Answer by Luiz_Thiago · Dec 05, 2014 at 05:19 PM
Oh God!!! Sry... I'm calling the ProcessSaveGame() inside the AddBuildingData(buildings[i]);
GOD!!!!
Ty dude!
Your answer
![](https://koobas.hobune.stream/wayback/20220613173303im_/https://answers.unity.com/themes/thub/images/avi.jpg)