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 /
This question was closed Jul 09, 2020 at 08:17 AM by pako for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by $$anonymous$$ · Dec 25, 2017 at 09:29 AM · level load

How to save the current scene the player is in using PlayerPrefs?

In my game players can move between different scenes, but if the game saves and loads it teleports the player to the right position, but the scene is the same.

I need a way to save current scene and load it.

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

  • Sort: 
avatar image
4
Best Answer

Answer by pako · Dec 25, 2017 at 10:33 AM

  1. In the first scene to load create an Empty GameObject and name it "SceneHandler".

  2. Create a new tag "SceneHandler" and add it to the "SceneHandler" GameObject

  3. Add the following "SceneHandler" script to the "SceneHandler" GameObject

  4. See the CustomScript script below for example usage

    using System.Collections; using UnityEngine; using UnityEngine.SceneManagement;

    public class SceneHandler : MonoBehaviour {

      void Awake()
         {
             DontDestroyOnLoad(gameObject);
         }
     
         public void SaveScene()
         {
             int activeScene = SceneManager.GetActiveScene().buildIndex;
     
             PlayerPrefs.SetInt("ActiveScene", activeScene);
         }
     
         public void LoadScene()
         {
             int activeScene = PlayerPrefs.GetInt("ActiveScene");
     
             //SceneManager.LoadScene(activeScene);
     
             //Note: In most cases, to avoid pauses or performance hiccups while loading,
             //you should use the asynchronous version of the LoadScene() command which is: LoadSceneAsync()
     
             //Loads the Scene asynchronously in the background
             StartCoroutine(LoadNewScene(activeScene));
         }
     
         IEnumerator LoadNewScene(int sceneBuildIndex)
         {
             AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneBuildIndex);
             asyncOperation.allowSceneActivation = false;
     
             while (asyncOperation.progress < 0.9f)
             {
                 yield return null;
             }
     
             asyncOperation.allowSceneActivation = true;
     
         }
     }
     
     
    

    using UnityEngine;

    //EXAMPLE USAGE OF Saving/Loading of Active Scene

    public class CustomScript : MonoBehaviour { private SceneHandler sceneHandler;

      void Awake ()
         {
             sceneHandler = GameObject.FindGameObjectWithTag("SceneHandler").GetComponent<SceneHandler>();
         }
     
         void CustomMethod()
         {
             sceneHandler.SaveScene();
     
             sceneHandler.LoadScene();
         }
     
     }
     
     
       
    
Comment
Add comment · Show 3 · 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
avatar image PoorTree · Nov 04, 2018 at 08:24 PM 0
Share

Hello, I know it may seem like a stupid question, but how could I apply this script in a game that each scene depends on buttons to be able to change scenes?

avatar image pako PoorTree · Nov 05, 2018 at 07:25 AM 0
Share

@PoorTree you should post a new question, where you can provide a link to this one.

FAQ

avatar image samf1111 PoorTree · Jul 08, 2020 at 06:55 PM 0
Share

use unity's OnClick function.

Follow this Question

Answers Answers and Comments

75 People are following this question.

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

Related Questions

Help With Level Load 1 Answer

How to make an exit level trigger? 3 Answers

How do I load previous levels individually in the build settings in unity3d 0 Answers

UnityAds and LevelLoad Problem 0 Answers

What is the best way to detect level load on additive/asnyc level loads 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