- Home /
Coroutine executes unrelated object's code
I implemented a Game Manager which loads levels based on a persistent style. Thus it never gets destroyed.
I use a system of doors which call the LoadLevel function. Sometimes it flawlessly loads the next scene. How ever sometimes it behaves bizarrely.
It behaves as follows: Enter door B -> A, Add both unloader and loader coroutine to list, progress wont reach 0.9 Then the bizzare part happens: Enter Door B -> C (which wasnt even entered by the player), adds its regarding unload and load coroutine and finisih all four piled couroutines in one go.
You can see the correlating debug message in the picture
public class GameManager : MonoBehaviour
{
// Public
public static GameManager instance { get; private set; }
public GameObject loadingScreen;
// Private
private SceneIndex currentLevel = SceneIndex.Scene1;
private void Awake()
{
instance = this;
SceneManager.LoadSceneAsync((int)SceneIndex.TitleScreen, LoadSceneMode.Additive);
}
[SerializeField]
private List<AsyncOperation> scenesLoading = new List<AsyncOperation>();
public void LoadGame()
{
loadingScreen.gameObject.SetActive(true);
scenesLoading.Add(SceneManager.UnloadSceneAsync((int)SceneIndex.TitleScreen));
scenesLoading.Add(SceneManager.LoadSceneAsync((int)SceneIndex.Scene1, LoadSceneMode.Additive));
StartCoroutine(GetSceneLoadProgress());
}
public void LoadLevel(SceneIndex si)
{
loadingScreen.gameObject.SetActive(true);
Debug.Log("Loading Debug: Unload Active Scene: " + currentLevel.ToString());
Debug.Log("Loading Debug: Load Next Scene: " + si.ToString());
scenesLoading.Add(SceneManager.UnloadSceneAsync((int)currentLevel));
scenesLoading.Add(SceneManager.LoadSceneAsync((int)si, LoadSceneMode.Additive));
currentLevel = si;
StartCoroutine(GetSceneLoadProgress());
}
IEnumerator GetSceneLoadProgress()
{
Debug.Log("Size of sceneslist: " + scenesLoading.Count);
scenesLoading[0].priority = 2;
for (int i = 0; i < scenesLoading.Count; i++)
{
if (scenesLoading[i] != null)
{
while (!scenesLoading[i].isDone && scenesLoading[i] != null)
{
Debug.Log(scenesLoading[i].progress);
yield return null;
}
}
}
loadingScreen.gameObject.SetActive(false);
Debug.Log("Level Loading Finished");
scenesLoading.Clear();
}
}
Door Script:
public class DoorScript : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField]
private SceneIndex nextScene;
private bool canEnter = false;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (canEnter && Keyboard.current[Key.Space].wasPressedThisFrame)
{
canEnter = false;
Debug.Log("Debug: This door was entered: " + gameObject.name);
GameManager.instance.LoadLevel(nextScene);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
canEnter = true;
}
}
just check first that you don't actually end up in multiple instances of you GameManager. You should be using singleton pattern, but you are not (code is missing the check if instance of the GameManager already exists and if does then this new one should be destroyed). In case you end up with multiple instances then the static instance reference will change to a new GameManger and can cause some unexpected behavior..
If this is not the issue then let's debug further...
Hi, you are indeed right, however, the GameManager is in a persistent scene thus it shouldnt be the issue. Confor$$anonymous$$g the pattern still returns the same issue. I have a dirty hack for it but I am not satisfied with it.