Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by worldofcars · Jan 05, 2012 at 08:25 PM · sceneloadlevellast

Load last scene/level

Hello. I have a simple question. I am making a game and I will thet if the player restart the game, it should be able to load the last scene/level with a button, where he played last time. Is this possible? And how? A script will be good;).

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

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by by0log1c · Jan 05, 2012 at 09:48 PM

Application.LoadLevel(Application.loadedLevelName);
Application.LoadLevel(Application.loadedLevel);

EDIT: My mistake, I didn't realize you want this to work over different game sessions. What you could do is have a script that saves Application.loadedLevel to PlayerPrefs and have the first scene have nothing but a script that'll retrieve the PlayerPrefs and load said level.

Pretty simple, really, here's untested code, to put once on the initial scene, where the player cannot go back.

//LevelLoadManager.js private var currentLevel:int = -1;

function Awake():void { //retrieve and load last level played. DontDestroyOnLoad(gameObject); currentLevel = PlayerPrefs.GetInt("LastLevelLoaded"); Application.LoadLevel(currentLevel); }

function Update():void { //save the current level if it seems to have changed. if(Application.loadedLevel != currentLevel) { currentLevel = Application.loadedLevel; PlayerPrefs.SetInt("LastLevelLoaded",currentLevel); } }

Comment
Add comment · Show 1 · 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 worldofcars · Jan 06, 2012 at 07:19 PM 0
Share

is not working

avatar image
0

Answer by inkspots · Jan 05, 2012 at 09:59 PM

You need to store a piece of information in some way, You can do this by using PlayerPrefs this wil save a specific piece of information to the disk. and you can load this when ever you want:

for example the player ends in level 10 and the name of the scene is level10 you can use this to save it:

PlayerPrefs.SetString("lastlevel", "level10";

and you can call this info by using:

PlayerPrefs.GetString("lastlevel")

and to put it into context...

var lastlevel = PlayerPrefs.GetString("lastlevel"); Application.LoadLevel (lastlevel);

before the player starts in this level you already need to save this in advance (what if the player would quit the game abruptly, then this data would also be lost) also keep in mind that you need to be more specific when calling information when multiple players would use the application

for more details check... http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

Comment
Add comment · Show 4 · 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 worldofcars · Jan 05, 2012 at 10:20 PM 0
Share

Thank you for the help. I will try this tomorrow and I hope this will work ;) thanks

avatar image worldofcars · Jan 06, 2012 at 07:02 PM 0
Share

noone of this scripts are working:( is there anyone with a working script or description?

avatar image inkspots · Jan 07, 2012 at 12:29 PM 0
Share

What code do you have so far ?

avatar image worldofcars · Jan 07, 2012 at 12:38 PM 0
Share

at the moment I don t have any code. Your code isnt working. $$anonymous$$aybe you have an other one?

avatar image
-1

Answer by worldofcars · Jan 06, 2012 at 07:02 PM

no one of this scripts are working:( is there anyone with a working script or description?

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 Lo0NuhtiK · Jan 06, 2012 at 09:05 PM 1
Share

All these answers people posted should have been plenty of help to get you going.
Just to simply restart the level, use Application.LoadLevel(Application.loadedLevel)
If you're wanting to reload the last played level after the player has quit, and then came back to the game, then you're going to have to go with something like what @BY0LOG1C suggested and playerprefs or some other way of saving and retrieving information.
If none of the codes people posted are working right for you, modify it and make it work how you want it to.
Otherwise try This or something else like it to find someone to program for you ; or use the search bar to try and find other stuff that you can piece together to get it working.

avatar image worldofcars · Jan 07, 2012 at 08:10 AM -1
Share

But it will be easyer if anyone post me a good code

avatar image Lo0NuhtiK · Jan 07, 2012 at 08:16 AM 1
Share

....uh-huh.

avatar image
0

Answer by snowconesolid · Jan 05, 2012 at 09:00 PM

use an if statment

if (Input.GetButtonDown("Jump"))

{ Application.LoadLevel ("The name of your level"); }

The "Jump" button is referring to the Space key if you want you can also use Input.GetKeyDown(KeyCode.whatever key you want pressed))

If you want to load the previous level each time, just make separate scripts. So for example lets say you have 4 levels and you want to go back one level each time the user presses the A key.

so if your in level 4, the script you would have would look like this

if (Input.GetKeyDown(KeyCode.A))

{ Application.LoadLevel (3); }

This will take you back to level 3, now you want to go to level 2, so in your level 3 the script would be this

if (Input.GetKeyDown(KeyCode.A))

{ Application.LoadLevel (2); }

and so on. so Just make the scripts different for each level, be sure that each script is its own separate script and your not just editing the same one over and over again.

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 worldofcars · Jan 05, 2012 at 09:25 PM 0
Share

You don't understand my question. I want it like this: If the player reach level 10 and close the game and restart it after a few $$anonymous$$utes, than I want this automaticly to load level 10 and not 0 ... do you understand this now?

avatar image snowconesolid · Jan 05, 2012 at 09:43 PM 1
Share

ahh I see, ok I know what your saying now. Thats a good question. Im not very sure on this one, but you might need to use

Application.LoadLevel(Application.loadedLevel)somehow. but still im not to sure how to get this working

maybe this can help you? http://answers.unity3d.com/questions/198878/restart-current-level.html

avatar image worldofcars · Jan 05, 2012 at 10:22 PM 0
Share

Thank you for the answers. I will see this tomorrow;)

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

8 People are following this question.

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

Related Questions

Multiple Instances of My Current Scene? 0 Answers

Current scene number 2 Answers

How can I save the player's progress in-game? 1 Answer

How to see what level is running? 2 Answers

What is the Difference betwwen a "Level" and a "scene" 2 Answers


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