- Home /
Can I use one script to toggle between scenes?
I have this script that I used to toggle the player between one scene and another scene. The script works by calling a function when a certain button is pressed (this function switches the scenes). Take a look:
 public class ToggleScript : MonoBehaviour
 {
     public Transform normalPlayer;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Q))
         {
             ReloadScene();
         }
     }
 
     public void ReloadScene()
     {
         SceneManager.LoadScene("AltScene");
         if (Input.GetKeyDown(KeyCode.E))
         {
             LoadOrigional();
         }
     }
 
     public void LoadOrigional()
     {
         SceneManager.LoadScene("NormalScene");
     }
 }
When I run this script, I start in "NormalScene" (as that's where the game starts). When I press Q, the game takes me to "AltScene" by calling the ReloadScene() function, but when I press E to take me back to "NormalScene" nothing happens. Can someone explain to me why this happens? On a side note, is there any way for me to make sure the player stays in the same positon (so if my player was at X-Coordinate 70 when I switch the scenes I want it to stay at X-Coordinate 70 when the scene is put on again. This issue is not the priority). Thank you so much for your help :)
you are reading for get key down for E in a one frame function, which means you need to press E and Q frame perfect simultaneous to make it work, which is very very difficult if not generally impossible...instead you should to do something like this
  public class ToggleScript : MonoBehaviour
  {
      public Transform normalPlayer;
  
      // Update is called once per frame
      void Update()
      {
          if (Input.GetKeyDown(KeyCode.Q))
          {
              ReloadScene("Altscene");
          }
          else if(Input.GetKeyDown(KeyCode.E)
          {
              ReloadScene("NormalScene");
          }
      }
  
      public void ReloadScene(string s)
      {
          SceneManager.LoadScene(s);
      }
  
id also recommend you look into things like build indexes and GetActiveScene() which will make this a piece of cake...for example
 void Update()
 {
   if(Input.GetKeyDown(KeyCode.Q) || Input.GetKeyDown(KeyCode.E))
   {
     LoadOther();
   }
 }
 
 public void LoadOther()
 {
   Debug.Log("loading");
   int i = SceneManager.GetActiveScene().buildIndex;
   int n = 0;
   if(i = 0)
   {
     n = 1;
   }
   SceneManager.LoadScene(n);
 }
then you can assign the scenes in the build settings and you're good to go, the advantage with using build indexes is that it makes it really easy to build a system to work with any number of scenes for example levels in a game
Answer by SamyBoyJim · Oct 03, 2021 at 04:52 AM
You need to put the
 if (Input.GetKeyDown(KeyCode.E))
 {
     LoadOriginal();
 }
in the update method because once you call the reload scene then it checks for the key E once and then ends the function.
Side Note: I assume that this script is affected by DontDestroyOnLoad() function so you can get a reference to the player transform at awake and before you call the load functions, store the player's x in a variable and set the x to that variable's value with a new Vector3 once you travel to the new scene.
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Do i need to make a Text variable for each variable i will use in the UI? 1 Answer
How can I make my navmesh agent stop in between travel to destination points? 0 Answers
Unity 2020.3.21f1 running slow 2 Answers
Need help in the stone throwing mechanic with aiming for mobile in Unity3d 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                