Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reloading a Scene when I die doesn't work for the second level.

Discussion in '2D' started by AmateurPrograming, Feb 4, 2021.

  1. AmateurPrograming

    AmateurPrograming

    Joined:
    Dec 27, 2020
    Posts:
    5
    For some reason this works for the first level but not the second.

    I am only using 3 lines of code to respawn the player.

    Code (CSharp):
    1.  public void Respawn()
    2. {
    3.        Time.timeScale = 1.0f;
    4.        SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
    5.        player.transform.position =
    6.        GameObject.FindGameObjectWithTag("SpawnPoint").transform.position;
    7. }
    Maybe when it reloads the scene, it doesn't add the player into it because it wasn't there to begin with? The player (and camera) starts out in the first level. Then I move the player and camera to the next level.

    Maybe instead of transferring my player and camera over to the next level I could just have a copy of them there and just switch scenes without moving them. I think this is clunky and not efficient (Problems with storing previous stats e.g. health).
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    I would just do a copy for each level. As for stats and inventory or w/e, just reload the PlayerPrefs. So everytime a scene is loaded you can have a script use PlayerPrefs.GetFloat or w/e to load all of those in each level. Every time you get a stat increase or a new item, you use PlayerPrefs.SetFloat so it all saves properly.
     
  3. AmateurPrograming

    AmateurPrograming

    Joined:
    Dec 27, 2020
    Posts:
    5
    I see, thanks!

    However is making a copy of the player standard? Seems a bit inefficient, I thought there would be a way to transfer the player over and just be able to reload the player along with the scene.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    I do it for all of my scenes. I see no inefficiencies on my performance. If you do it and start to see some drop in the profiler, then you can change it later. Think of the Player gameobject as a vessel or a shell. Put that vessel in the proper spot each time and then load in the scripts and information needed to bring it to life.

    You can probably do it the way you are thinking or the way i mentioned, but you shouldnt have to worry about performance drop unless you are making a large/complex game. You probably wont notice a thing either way.