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
1
Question by Matthew Dennis · Apr 21, 2011 at 10:15 AM · loadlevelapplication

Loading previously played level

How would i go about to load a previously played level? For example i have got a level select screen full of 15 levels. If you die in a level a new scene will be loaded to basically say you have failed with a play again button on. If i pressed that button i want to be able to go back to that level(scene) previously played.

I have an idea where when "Application.LoadLevel();" is called when i select a level, is there a way i can store that level to another variable that can be used later on for example;

selectedLevel = loadedLevel(1);

So when i come to clicking that play again button, i would simply call the selected level.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by FrHaYwOrKs · Apr 21, 2011 at 10:43 AM

I hope I have well understood your problem, because I think you could use a pretty simple solution:

1) Create in your first scene an empty object with a script attached.

2) Edit the script to let that object survive when a new level get loaded:

var lastLevelPlayed : int = 0;

function Awake() { DontDestroyOnLoad(transform.gameObject); }

3) Now the object can store global variables in its script, like the last level played and you can access it reading and writing from every other scene you will load.

To access the script and the variable inside it you just have to use the GameObject.Find() function and then the GetComponent() function to get the script itself.

So, supposing you called the empty object "LevelSelector" and the script "LevelScript", you will do:

// Reading levelSelected = GameObject.Find("LevelSelector").GetComponent("LevelScript").lastLevelPlayed;

// Writing GameObject.Find("LevelSelector").GetComponent("LevelScript").lastLevelPlayed = 6;

I hope it helps! ^^

[FIRST EDIT (To answer the comment)]

In every scene that represents a level you will need a script to acrivate at level load, like this, I think (supposing you are loading level 3 and using the same names that I proposed before):

function Start() {
GameObject.Find("LevelSelector").GetComponent("LevelScript").lastLevelPlayed = 3;
}

So you set the current level used, then you play, and you die badly... :)

Now when you call to know which level was played last, in your loader script you just have to put:

levelSelected = GameObject.Find("LevelSelector").GetComponent("LevelScript").lastLevelPlayed;

And you will get 3 in levelSelected value... :)

I hope it is clearer now! ^^

[/FIRST EDIT]

Comment
Add comment · Show 5 · 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 Matthew Dennis · Apr 21, 2011 at 10:57 AM 0
Share

It does help yes, but just a few questions. I have created the gameobject with the script onto level 1 for example and then when i die, and click play again, how would i go about to call the lastLevelPlayed? Now only that it seems that the number of the lastLevelPlayed may not seem to be updating.

avatar image FrHaYwOrKs · Apr 21, 2011 at 11:05 AM 0
Share

I edited the post with some more code samples that could help you better understanding the solution... :)

avatar image Matthew Dennis · Apr 21, 2011 at 11:48 AM 0
Share

You my sir are awesome!

avatar image FrHaYwOrKs · Apr 21, 2011 at 01:14 PM 0
Share

Glad to help, when I can! :)

avatar image mogambo · Aug 26, 2014 at 08:04 AM 0
Share

Hi, Does this solution also work for Android Back $$anonymous$$ey implementation ?

avatar image
4

Answer by Cyb3rManiak · Apr 21, 2011 at 10:46 AM

The simplest (but not smart in the long run) way to do it is use Application.LoadedLevel or Application.LoadedLevelName. Application.LoadLevel has 2 overloads, so a line like

Application.LoadLevel(Application.LoadedLevelName);

or

Application.LoadLevel(Application.LoadedLevel);

or even

Application.LoadLevel(Application.LoadedLevel - 1);
Application.LoadLevel(Application.LoadedLevel + 2);

will work.

A better way is like you said - keep track of the level you're in. To load the level you must have a place that calls Application.LoadLevel with some parameter. Simply save that parameter. If you use the level name - save it in a string variable. If you're using the level number - save the number in an int right after the level was loaded (You can use Application.OnLevelWasLoaded, or just save the variable right when you call the Application.LoadLevel method).

Also - You noted you have a level select screen. This means that you already have some sort of list of levels. You can save the index YOU use to load the scene in a script which has DontDestroyOnLoad in it - that will make the script stay even if you switch scenes, and you can ALWAYS find it and see what level you loaded last.

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 Matthew Dennis · Apr 21, 2011 at 10:59 AM 0
Share

I kinda get what you mean yes, its just the matter of now doing it :P

avatar image
1

Answer by lub0v · Jul 29, 2013 at 03:18 PM

Why not just use a static variable with the number of the last played level?

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

1 Person is following this question.

avatar image

Related Questions

Application.Loadlevel("Level Name"); not working on android 2 Answers

Weird behaviour in Application.LoadLevel() 0 Answers

How to create a complicated game over scene ? Help 1 Answer

Teleporting/application.loadlevel 1 Answer

Play Scene help 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