Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by mirceaculita · Jul 18, 2017 at 06:38 AM · 2d gamesave scene

How do I save the scene that I last played, and then load it at next run-time?

  So I am currently working on a multi-level 2D game that requires you to play trough a multitude of short levels, kind of like that old game Red Ball. The difference being that i don't want to have a level selector, but I want the game to remember the last played level and then load it at runtime. I don't need complex saves with positions or health or objects, all I want to save is the last played scene and then load it. The game is based on basic scripts of movement and scene changing.

  I'd like to add the fact that I am a COMPLETE newb at scripting so I may need simple explanations.


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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Jul 18, 2017 at 06:39 AM

That's a perfect scenario for using PlayerPrefs. https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data

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
avatar image
1

Answer by Istros · Jul 18, 2017 at 07:11 AM

Hi!I prepared an example code for you.I tried to explain everything clearly I hope it is.You just need to have codes to call the 2 custom functions.Have a good day!

 using UnityEngine;
 using System.Collections;
 
 public class SceneTracker: MonoBehaviour 
 {
 
 public int lastloadedsceneIndex;//When building your game every scene has its own index number.Starts from 0.Default start scene is 0.(I will assume this is your main menu or scene loader so first level we will load will have index 1)
 
     void Awake() 
     {
         DontDestroyOnLoad(transform.gameObject);//This makes this gameobject to not to be destroyed when a new level/scene is loaded.So we can track which scene he/she finished lastly.
     }
 
     void Start()
     {
         lastloadedsceneIndex = PlayerPrefs.GetInt("LastLoadedSceneIndex");//When the game starts we get the last saved value for index.
     }
 
     public void UpdateLastLoadedSceneIndex()
     {
         lastloadedsceneIndex++;//We increase index by one.
 
         PlayerPrefs.SetInt("LastLoadedSceneIndex",lastloadedsceneIndex);//We save the last value for index.SetInt takes two arguments.First one is
 
         unique code for our save.Be sure to use same codes for Get and Set functions.Second parameter is the value.You can directly set it as a number but this is not our goal.
     }
 
     public void LoadNextScene()
     {
         Application.LoadLevel(lastloadedsceneIndex + 1);//This method is a little bit old but still works.In paratheses you can write level name(with using "") or level index(without "").
     }
 }
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 mirceaculita · Jul 18, 2017 at 11:24 AM 0
Share

hi, thanks for your answer but as I am not good with scripting I have no idea where I should use this script, I'm sorry if this is a stupid question, but can you tell me where I should use it?

avatar image Istros mirceaculita · Jul 20, 2017 at 03:34 PM 0
Share

Hi!'You need to create a Script and name it SceneTracker.Then copy everything and then paste it to the script.It is done but to use it you need to call it from a script.You can call it in 2 different ways but I will show only the first one.

1.Go to a script(or create) that changes the level when you want.

2.In the parameters type this:"public SceneTracker sT;"//We make a reference to SceneTracker Script.

3.Go to start function then type this:"sT = GetComponent();"//We get the SceneTracker Script in the scene.

4.Create a function that calls the SceneTracker.Example:

public void ChangeScene() { st.LoadNextScene();//We call the LoadNextScene function from this script. }

5.Create a empty gameobject and put these 2 scripts into that gameobject.

Done!

Note:For now we do not update the index number.You should create another method to update the index number(Or just call the UpdateLastLoadedSceneIndex function like above)

avatar image djlevana18 Istros · Dec 21, 2020 at 11:30 AM 0
Share

Hello Istros are you available for chat? i have a few questions about you're script thank you indeed

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

73 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

Related Questions

2D 'Voxel' Game 1 Answer

Help with 2d Civ style map 0 Answers

Im getting an error ..Cannot implicitly convert type `UnityEngine.GameObject' to `BulletContrl' 3 Answers

How do I get the sprite pointing the right direction? 1 Answer

Android Native Screenshot function creats black Image 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