Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
0
Question by hollym16 · Nov 17, 2020 at 03:19 PM · sceneplayerprefssavekey

PlayerPrefs performing function even when it doesn't have that key

When I open my scene, I want to choose whether to display one GameObject, or another based on where I've just navigated from. On some buttons I have the code:

  public void Xmas(){
       MapKey = "OpenScene";
     PlayerPrefs.SetInt("OpenScene", 1);
     PlayerPrefs.Save();
      SceneManager.LoadScene("XmasStage");
      Debug.Log("KEY IS " + MapKey);
  }

On other buttons I have:

 public void BackToMap(){
      MapKey = "OpenMap";
     PlayerPrefs.SetInt("OpenMap", 1);
     PlayerPrefs.Save();
     SceneManager.LoadScene("XmasStage");
     Debug.Log("KEY IS " + MapKey);
 }

On the Awake function of the "XmasStage" scene, I check to see which key has saved and turn on the right GameObject accordingly:

  void Awake()
     {        
           if(PlayerPrefs.HasKey("OpenMap")){
    map.SetActive(true);
    XmasMenu.SetActive(false);
    Debug.Log("OPENING MAP");
 }
 if(PlayerPrefs.HasKey("OpenScene")){
   map.SetActive(false);
    XmasMenu.SetActive(true);
    Debug.Log("OPENING MENU");
 }
 Debug.Log("KEY IS " + MapKey);
 }

As you can see, I've got a lot of Debug.Logs to try and keep track of what's going on.

When I press the button that sets the key, the debug log shows the correct key.

When it loads the scene, it still shows the correct key but then performs both PlayerPrefs functions; in the debug log it says "OPENING MAP" then "OPENING MENU" meaning it will always end up activating the OPENING MENU GameObject

Can anyone tell me what I'm doing wrong?

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

Answer by rh_galaxy · Nov 17, 2020 at 03:43 PM

It will have both keys if you have run PlayerPrefs.SetInt and Save at any time before... The keys are never deleted unless you do it yourself. Perhaps what you need is another key (i.e prefs variable)

 PlayerPrefs.SetInt("LastOpen", 0); //meaning menu
 PlayerPrefs.SetInt("LastOpen", 1); //meaning map
 
 PlayerPrefs.GetInt("LastOpen", 0); //will give 0 (menu) if the key does not exist (only happens first time)
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 hollym16 · Nov 17, 2020 at 03:58 PM 0
Share

Thank you! I assumed that it would overwrite rather than just save both. Your method works, as does deleting the key just before you set it, so it only saves one key.

 PlayerPrefs.DeleteKey($$anonymous$$apKey);
avatar image
0

Answer by hollym16 · Nov 19, 2020 at 12:01 PM

@rh_galaxy I'm trying to implement your method but I'm a bit unclear on how it works. I understand that the GetInt will get the number based on what you SetInt, but how do you then declare what to do, based on that number?

In my previous script I used:

 if(PlayerPrefs.HasKey("OpenScene")){
 //Do something

Do I need to do something similar with GetInt, such as:

 if(PlayerPrefs.GetInt == 2){
 //Do something


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 rh_galaxy · Nov 19, 2020 at 12:23 PM 0
Share

Everything you do with PlayerPrefs involves a Key, that's a name that you associate with the Data. Data can be a string or an int or a float or other types.

SetInt("LastOpen", 1) sets the Key to LastOpen and the data to an int that has value 1. GetInt("LastOpen", 0) gets the value 1 from the Key LastOpen, but if the key does not exist it returns the value 0.

PlayerPrefs.HasKey("LastOpen") will return true if the Key exists, regardless of the value, it can be 100 or -65 or 1. What you decide to do when a value is 0 or 1 or 5 is up to you in the code. You don't need to use PlayerPrefs.HasKey at all, just use GetInt or SetInt in your case.

avatar image hollym16 rh_galaxy · Nov 19, 2020 at 01:20 PM 0
Share

Thanks for the reply. So would I use something like this? key = "LastOpen";
PlayerPrefs.GetInt("LastOpen", 0); if(key== 0){ //Do something

avatar image rh_galaxy hollym16 · Nov 19, 2020 at 01:29 PM 0
Share

This is not so hard, from your original post, you would do:

 public void $$anonymous$$(){
     PlayerPrefs.SetInt("LastOpen", 1);
     PlayerPrefs.Save();
     Scene$$anonymous$$anager.LoadScene("$$anonymous$$Stage");
 }
 public void $$anonymous$$enu(){
     PlayerPrefs.SetInt("LastOpen", 0);
     PlayerPrefs.Save();
     Scene$$anonymous$$anager.LoadScene("$$anonymous$$enu");
 }
 
 int value = PlayerPrefs.GetInt("LastOpen", 0);
 if(value==1) {
     Debug.Log("OPENING X$$anonymous$$AS $$anonymous$$AP");
 }
 if(value==0) {
     Debug.Log("OPENING $$anonymous$$ENU");
 }

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

178 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 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 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

Saving for a Level Editor 1 Answer

How to save progress in the game? 1 Answer

Updating main menu using PlayerPrefs when collecting an object in different scene (level) 1 Answer

How to save gameobject values with respect to scene? 1 Answer

Highscore won't save 3 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