Loading progress logical error
Thanks guys for the great tutorial, there are some logical issues in the LoadingScreen function. Although this function is not used in the final version of the tutorial it's highly possible someone may take it as a reference, so I hope you can fix it.
To understand the problem I recommend to add some Debug messages in the function as the following:
IEnumerator LoadingScreen()
{
float totalProgress = 0;
//Iterate through all the scenes to load
for (int i = 0; i < scenesToLoad.Count; ++i)
{
Debug.Log("check loading of scene " + i + " :" + scenesToLoad[i].isDone);
while (!scenesToLoad[i].isDone)
{
//Adding the scene progress to the total progress
totalProgress += scenesToLoad[i].progress;
//the fillAmount needs a value between 0 and 1, so we devide the progress by the number of scenes to load
Debug.Log($"{i}=>total progress/count={totalProgress}/{scenesToLoad.Count}");
yield return null;
}
}
Debug.Log($"End of LoadingScreen coroutine!");
}
Issues:
-
The Idea of iterating over all scenes using for loop with this way is not the right way to do it, as all scenes will be loaded in parallel and the while loop will keep looping until the first scene get completely loaded and in the same time other scenes will get some progress as well which will not be counted in the totalProgress
-
In one iteration of the for loop, while loop will keep looping many times and it will add the progress of the same scene over and over again, so the previous debug code will debug something like: 0=>total progress/count=0.9/2 0=>total progress/count=1.8/2 0=>total progress/count=2.7/2
-
The value of the AsyncOperation.progress ranges from 0 to 0.9 not from 0 to 1, this is something that should be considered as well
-
Because you're loading the Gameplay scene in a single mode not additive, once the scene get loaded the mainMenu scene will get unloaded and the for loop function will stop at i=0 and will never go to the next iteration, so with the previous debugs you will get this message only check loading of scene 0 :False and will never get check loading of scene 1 :False
I hope you guys take those points in consideration and write a cleaner version of LoadingScreen function so the people can follow it in their games.