I’m trying to use an asynchronous coroutine to load level items while other stuff continues, then when loading is done I modify some of the items so they can be used for a specific level. Occasionally, the items fail to be modified, a problem which can make the level impossible to win but is difficult to debug because it happens so rarely. Running the modifications code thousands of times didn’t reproduce the bug, so the problem must be due to the coroutine sometimes failing to load everything before modifications are made despite the fact that the coroutine sets a flag when it finishes and the other code waits for that flag to be set before making modifications. Here’s the code I’m using for the coroutine (the first function sets it running and the second is the coroutine itself). Am I doing something wrong?
function LoadPrefabAsynchronously(Prefab:GameObject, PrefabPos:Vector3, PrefabRot:Quaternion, Parent:GameObject)
{
LPAFinished=0;
LPAprefab = Prefab; LPAprefabPos = PrefabPos; LPAprefabRot = PrefabRot;LPAParent = Parent;
StartCoroutine("LoadPrefabAsynchronouslyCoRoutine");
}
//co-routine for above
// Prefab to load needs to be in LPAprefab
// Parents it to LPAParent GO
// "Returns" instantiated GO by placing it in LPAprefabGO
function LoadPrefabAsynchronouslyCoRoutine()
{
yield WaitForEndOfFrame();
LPAprefabGO = Instantiate(LPAprefab,LPAprefabPos, LPAprefabRot);
LPAprefabGO.transform.parent = LPAParent.transform;
LPAFinished=1;
}