0
\$\begingroup\$

I have a Game Manager with a DontDestroyOnLoad function on it, before loading a scene I'm passing the variables needed to reset players position to a certain position. This is what my code looks like;

public IEnumerator LoadingNextLevel(int levelToLoad, string spawnPoint)
{
    pState.movingToNextLevel = true;
    fadeToBlack.SetTrigger("FadeOut");

    yield return new WaitForSeconds(0.5f);
    SceneManager.LoadScene(levelToLoad);
    fadeToBlack.SetTrigger("FadeIn");
    pState.movingToNextLevel = false;
    if (spawnPoint == "Red")
    {
        player.transform.position = GameObject.FindGameObjectWithTag("Red").transform.position;
    }
    else if (spawnPoint == "Blue")
    {
        player.transform.position = GameObject.FindGameObjectWithTag("Blue").transform.position;
    }
}

This doesn't give me any error, nor it does the job. What am I doing wrong?

\$\endgroup\$
  • \$\begingroup\$ try to update reference on player game object after loading new scene \$\endgroup\$ – Stranger in the Q Apr 25 '19 at 7:15
  • \$\begingroup\$ @StrangerintheQ it should be updating in Awake with FindGameObjectWithTag \$\endgroup\$ – E. Kinglet Apr 25 '19 at 12:00
  • \$\begingroup\$ @StrangerintheQ oh I see what you are talking about.. Ok I've tried to find the game object with tag instead of using player, I think that it should work but it still doesn't. \$\endgroup\$ – E. Kinglet Apr 25 '19 at 12:39
  • 2
    \$\begingroup\$ you are trying to find the spawn points before they are loaded into the next scene. I would first load the new scene then check for the positions to start \$\endgroup\$ – Justin Markwell Apr 25 '19 at 13:50
  • 1
    \$\begingroup\$ @JustinMarkwell: I think this could be expanded into an upvote-worthy answer. :) \$\endgroup\$ – DMGregory Apr 25 '19 at 13:58
1
\$\begingroup\$

A few different ways to handle but being this is in your persisting gamemanager code you can just add the async operation and first make sure the level is loaded. then you can find and set the spawns then change the positions.

public IEnumerator LoadingNextLevel(int levelToLoad, string spawnPoint)
{
    pState.movingToNextLevel = true;
    fadeToBlack.SetTrigger("FadeOut");

    yield return new WaitForSeconds(0.5f);
    AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(levelToLoad);
    fadeToBlack.SetTrigger("FadeIn");
    pState.movingToNextLevel = false;
    // Wait until the asynchronous scene fully loads
    while (!asyncLoad.isDone)
    {
        yield return null;
    }
    if (spawnPoint == "Red")
    {
        player.transform.position = GameObject.FindGameObjectWithTag("Red").transform.position;
    }
    else if (spawnPoint == "Blue")
    {
        player.transform.position = GameObject.FindGameObjectWithTag("Blue").transform.position;
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.