So I’ve set up the following code:
public class TravelManager : MonoBehaviour
{
[SerializeField] Transform havenRespawn;
[SerializeField] SceneSO persistentScene;
[SerializeField] PlayerController playerController;
[SerializeField] GameSceneManager gameSceneManager;
[SerializeField] LoadingScreenManager loadingScreenManager;
bool sceneDone = false;
IEnumerator respawnCoroutine;
private void Start()
{
GameEventsManager.current.onSceneDoneLoadingEvent += SceneDoneTrigger;
}
public void RespawnAtHaven()
{
if (respawnCoroutine != null)
{
StopCoroutine(respawnCoroutine);
}
respawnCoroutine = RespawnCoroutine();
StartCoroutine(respawnCoroutine);
}
private IEnumerator RespawnCoroutine()
{
gameSceneManager.LoadScene(persistentScene, true, null);
while (!sceneDone)
{
yield return null;
}
sceneDone = false;
playerController.TeleportPlayer(havenRespawn.position, havenRespawn.rotation);
loadingScreenManager.CloseLoadingScreen();
}
private void SceneDoneTrigger()
{
sceneDone = true;
}
}
Basically whenever GameSceneManager
is done loading a scene it triggers an event through GameEventsManager
. What I want to do is wait for that event to trigger after the gameSceneManager.LoadScene
line and once it triggers do the rest of the coroutine.
I had to set up a new boolean for this and it works fine, but I was wondering if there is a more direct way of listening to that event trigger in the coroutine.