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
0
Question by Sentinental · Aug 21, 2015 at 12:03 AM · playerprefsscene-loadingloading

Can you load a level through using PlayerPrefs?

Hello I have run into a small problem with my level loading. The game is a small 2D game where when you hit a trigger you lose the game and it opens a new scene where it says "try again". When you press "Try Again" I want to be able to load the last played level. I tried using player prefs so I did this:

"PlayerPrefs.SetString = Application.loadedLevelName;"

And then tried to call it in a different script with this line:

"Application.LoadLevel (PlayerPrefs.GetString);"

but unity didn't like it and i got an error message. I just need a way to load the last played level when you press a button. Any and all help is much appriciated.

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
4
Best Answer

Answer by Bunny83 · Aug 21, 2015 at 11:41 AM

Well, to store a string in PlayerPrefs you have to provide a "name" for the value you want to save. That name you have to use when you want to read the value back:

 // storing the current level
 PlayerPrefs.SetString("lastLevel", Application.loadedLevelName);
 
 // loading the stored level or if none was stored yet, the "defaultLevelName".
 Application.LoadLevel(PlayerPrefs.GetString("lastLevel", "defaultLevelName"));
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 Sentinental · Aug 21, 2015 at 01:36 PM 0
Share

Thank you all for the help, works perfectly now. All I had to do was use PlayerPrefs correctly

avatar image
0

Answer by Positive7 · Aug 21, 2015 at 12:06 AM

SetString() : http://docs.unity3d.com/ScriptReference/PlayerPrefs.SetString.html

GetString() : http://docs.unity3d.com/ScriptReference/PlayerPrefs.GetString.html

 PlayerPrefs.SetString(Application.loadedLevelName, Application.loadedLevelName);
 Application.LoadLevel(PlayerPrefs.GetString(Application.loadedLevelName));

A much easier way is using : http://docs.unity3d.com/ScriptReference/Application-loadedLevel.html

 Application.LoadLevel(Application.loadedLevel);


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 Sentinental · Aug 21, 2015 at 11:37 AM 0
Share

Yes I know but the problem is when you lose in a certain level a lose screen comes up where you press try again and I want to be able to load the last played level not a specific one every time. So regardless of which level or scene you lost in you load that level.

So loading the current level wont work it has to be the last played level.

avatar image Bunny83 · Aug 21, 2015 at 11:44 AM 0
Share

@Positive7: sorry, but i can't upvote your answer at the moment as your code makes no sense at all. Are you sure you understand PlayerPrefs yourself? ^^

avatar image Positive7 · Aug 21, 2015 at 11:46 AM 0
Share

Ohh, so "Try Again" is in a new scene.Then loadedLevel won't work for you, Sorry about that. But the SetString() GetString() should work.

@Bunny83 Yeah, I realized it's not setup correctly(after seeing your answer). It's my mistake, Sorry about that too. In my defence it works in a single scene tho. :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problems with Playerprefs script 1 Answer

How to select scene based on device screen size? 1 Answer

Saving Stream to PlayerPref then unpack in new scene. 1 Answer

Storing in PlayerPref based on timer 1 Answer

Saving with PlayerPrefs? How it works? 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