Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by mare65 · Apr 02, 2017 at 09:57 AM · scene-loadingscene-switchinglevels

Load previous scene

Hello, after completing different tutorials, I ventured out on my own to try to see what I could create, but I am stuck. I have a player and when it collides with a star the player leaves scene1 and is sent to a bonus scene with a timer. Bonus scene works fine with timer running, but when the time runs out how do I get back to previous scene1 where the player left off? Eventually I will have random levels loading and whenever bonus object is triggered, switch to bonus scene, the time runs out and player returns to previous level. Looking for some code examples. I think it has something to do with Var, PlayerPrefs and maybe DontDestroyOnLoad, but I am confused what to write and where to put this code. This is my script for bonus scene and timer.

script attached to star object.

public class BonusStar : MonoBehaviour {

 void OnTriggerEnter2D(Collider2D other)
 {
    if (other.gameObject.tag == "Player")
  {
    SceneManager.LoadScene("BonusScene");
   }

}

script timer on Bonus scene.

public class Timer : MonoBehaviour {

 public Text timer;
 public float countDown = 15;

 void Start()
 {
     timer = GetComponent<Text>();
  }

 void Update()
 {
     countDown -= Time.deltaTime;
     timer.text = countDown.ToString("f0");
     if (countDown <= 0)
     {
         //back to previous scene1?
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by kornstar83 · Jun 08, 2017 at 05:08 PM

reloading the last scene is relatively easy, just store the name of the active scene into a string and when the bonus scene calls the SceneManager.LoadScene (string) just pass in the saved string name,

but while going in between scenes the values can be lost so you will either need to have them on a script that is set to DontDestroyOnLoad or simply save the string into the PlayerPrefs.

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag == "Player") {
         PlayerPrefs.SetString ("lastLoadedScene", SceneManager.GetActiveScene ().name);
         SceneManager.LoadScene ("BonusScene");
     }
 }

Then on your Bonus Scene Script you would put,

  public Text timer;
  public float countDown = 15;
  void Start()
  {
      timer = GetComponent<Text>();
   }
  void Update()
  {
      countDown -= Time.deltaTime;
      timer.text = countDown.ToString("f0");
      if (countDown <= 0)
      {
          string sceneName = PlayerPrefs.GetString("lastLoadedScene");
          SceneManager.LoadScene(sceneName);//back to previous scene1?
      }
  }

That should solve your scene loading problem but having the player go back to where they where when the level scene is loaded again is a whole other bucket of worms.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Modify Scene origin before loading scene? Or LoadLevelAdditive @ certain world position 0 Answers

Change Scene keeping Character and GUI Canvas 1 Answer

Scene loading problem 5 Answers

Help with error message "Overwriting the same path as another open scene is not allowed" 0 Answers

,Enums not being passed between scripts properly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges