Search Unity

How to go to previous scenes

Discussion in 'Visual Scripting' started by patroni12, Sep 12, 2022.

  1. patroni12

    patroni12

    Joined:
    Apr 28, 2022
    Posts:
    4
    Hi i'm new to unity and visual scripting and I'm making a little game.
    I have a scene (scene 1)that will be accessed by other scenes (scene 2 and 3).

    How can I make that when I'm in scene 1 and i press tge back bytton, I go to either scene 2 or 3 depending of which one led me to scene 1 ?

    Here is what I've tried

    -Unload a scene async (worked but I couldn't interact with the previous scene).

    -put the scene content into a game object and disabling it (the same result)

    I tried to use enums but I don't know how to do that with visual scripting.
     
  2. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    Simple solution - save the last loaded scene's index. It can be in Player Prefs or in a variable inside some script
     
    patroni12 likes this.
  3. patroni12

    patroni12

    Joined:
    Apr 28, 2022
    Posts:
    4
    Can you explain to me how to do that ?
     
  4. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.SetInt.html

    This should help you. Whenever you load a scene save the current scene's index under say "previousSceneIndex" key.

    Then, on your back button method, you can get value that is under that key and open a scene with that index.
     
    patroni12 likes this.
  5. patroni12

    patroni12

    Joined:
    Apr 28, 2022
    Posts:
    4
    Thank you very much
     
  6. patroni12

    patroni12

    Joined:
    Apr 28, 2022
    Posts:
    4
    So i figured out another way.
    i put it here for those who are strugling like me
    All i had to do was load a scene additively by doing so the previous scene would still be active so i just had to tell that when i click on a button it "get active scene" then "get build index" and "load scene" from that build index.