Search Unity

Question need help detecting scene load

Discussion in 'Getting Started' started by stain2319, Apr 2, 2021.

  1. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    417
    Not sure what I am doing wrong here but I am trying to reposition my player object when a particular scene loads (Transitioning from start/options screen to actual game scene.)

    I have OptionsScene loaded and the player object exists with DontDestroyOnLoad(gameObject) and I am able to load the next scene when clicking my "start game" button but when the scene loads it doesn't reposition the player and I am not sure what I am missing.

    relevant code:

    Player:
    Code (CSharp):
    1. public void Start()
    2. {
    3. SceneManager.activeSceneChanged += ResetPosition;
    4. }
    5.  
    6. public void ResetPosition(Scene current, Scene next)
    7.     {
    8.         Debug.Log("Scene Change");
    9.         string currentName = current.name;
    10.         string newName = next.name;
    11.  
    12.         if (newName == "game_scene") {
    13.             this.transform.position = startPos; }
    14.        
    15.     }
    Start Button Code:

    Code (CSharp):
    1. void LoadGame()
    2.     {
    3.         SceneManager.LoadScene("game_scene");
    4.         Scene scene = SceneManager.GetSceneByName("game_scene");
    5.         SceneManager.SetActiveScene(scene);
    6.  
    7.     }
    8.  
    9.  
    When I hit the button, it successfully loads the "game_scene" scene as it should, but it does not reposition the player and I get the following in the console:

    ArgumentException: SceneManager.SetActiveScene failed; scene 'game_scene' is not loaded and therefore cannot be set active
    UnityEngine.SceneManagement.SceneManager.SetActiveScene (UnityEngine.SceneManagement.Scene scene)

    But it obviously is loaded, because I'm in it.

    Is there a better way to do this? I just want to move my player to a specific position when that scene loads...
     
  2. https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
    Which means your code
    Code (CSharp):
    1. Scene scene = SceneManager.GetSceneByName("game_scene");
    2. SceneManager.SetActiveScene(scene);
    Runs before the scene is loaded since the scene load happens when all of the MonoBehaviours are finished and before the next frame.
     
  3. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    417
    Ok great. So how can I work around it? Is there a better way to detect when a new scene has loaded and move the player when it loads? All the information I can find on this is really confusing.
     
  4. Unless I misunderstand something you do not need these two lines at all. You are already checking the scene name in the ResetPosition method.

    But I wouldn't do this on this way. Probably it is better way if you put a MonoBehaviour in the new scene and in the Awake method you search for your player object with GameObject.Find. It searches through all active scenes. When you got it, you can reposition your player calling the ResetPosition method directly.
     
    stain2319 likes this.