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
1
Question by squshy · Jul 27, 2016 at 07:06 PM · c#playerprefssaving

Player.Prefs not working on android[solved]

Basically my Player.prefs control my players Coins and lives it works flawlessly on unity and in a windows build but when i convert it into android id doesn't work at all not even in the same scene i really do not understand where im going wrong

heres my life manager

 public class LivesManager : MonoBehaviour {
 
     public int StartingLives;
     private int LifeCounter;
     private LivesManager LifeSystem;
     
     public GameObject Timer;
    
     public Transform SpawnPoint;
 
     private Text thetext;
 
     void Start()
     {
         LifeSystem = FindObjectOfType<LivesManager>();
         thetext = GetComponent<Text>();
 
         if (PlayerPrefs.GetInt("life") <= 0)
         {
             StartCoroutine(WaitTime());
         }
 
       }
 
     void Update()
     {
         if (PlayerPrefs.HasKey("life"))
         {
             LifeCounter = PlayerPrefs.GetInt("life");
 
         }
         
 
         thetext.text = "x " + LifeCounter;
     }
 
 
 
     public void GiveLife5()
     {
         LifeCounter += 5;
 
         
             PlayerPrefs.SetInt("life", LifeCounter);
             PlayerPrefs.Save();
         
     }
     public void GiveLife()
     {
         LifeCounter++;
 
        
            PlayerPrefs.SetInt("life", LifeCounter);
             PlayerPrefs.Save();
         
     }
 
     public void Takelife()
     {
         LifeCounter--;
         
             PlayerPrefs.SetInt("life", LifeCounter);
             PlayerPrefs.Save();
         
     }
 
     public void OnButtonPressChallenge()
     {
         if (LifeCounter > 3)
         {
 
           
             Debug.Log("Nope");
             return;
 
         }
             Application.LoadLevel("ChallengeMode");               
     
     }
 
     public void OnButtonPressStore()
     {
         Application.LoadLevel("Store");
     }
 
     public void OnButtonPressMission()
     {
         Application.LoadLevel("Zone1");
     }


This is accessed by multiple scripts including this welcome button which adds 5 lives.

 public class Welcome : MonoBehaviour {
 
     private int Clicked;
     public GameObject Button;
 
     private LivesManager LifeSystem;
 
     void Start()
     {
         LifeSystem = FindObjectOfType<LivesManager>();
 
         if (PlayerPrefs.HasKey("welcome"))
         {
             Clicked = PlayerPrefs.GetInt("welcome");
         }
     }
 
     public void OnButtonPress()
     {
         Debug.Log("Welcome");
         LifeSystem.GiveLife5();
         Clicked++;
         PlayerPrefs.SetInt("welcome", Clicked);
         PlayerPrefs.Save();
       }
 
     void Update()
     {
         if (Clicked == 1)
         {
             Debug.Log("goneforever");
             Destroy(Button);
         }
     }
 
 }
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 saschandroid · Jul 28, 2016 at 06:02 AM

I guess the reason is, if you installed the game on android you don't have any PlayerPrefs when starting the game. So if you use 'HasKey' in Start() it returns false and if you're trying to get a value it returns the default value (e.g. 0, because you didn't set a default value yourself like: GetInt("life", 7)). When playing the game in Editor or Windows you probably already have saved PlayerPrefs from a different version of your game (afaik they don't get deleted if you play in Editor mode or build a new Windows version, but re-installing on Android deletes previously saved PlayerPrefs ... so they are empty again after a new build). Save the PlayerPrefs with your default values once, after installing and starting the game.

Comment
Add comment · Show 2 · 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 squshy · Jul 28, 2016 at 02:09 PM 0
Share

I would like the lives to start at 0 upon first start would i still need to set this? and they reset even after welcome button is pressed and scene is changed but welcome button does not reappear.

avatar image squshy · Jul 28, 2016 at 02:38 PM 0
Share

This works now thanks.

avatar image
0

Answer by Denscrivent · Jul 28, 2016 at 04:21 AM

try debug.log the prefs and use adb logcat to check it when you play the game in android device

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

7 People are following this question.

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

Related Questions

Save/Load Animation State of Instantiated Prefabs 0 Answers

PlayerPrefs not saving 1 Answer

Adding Value to Already Saved Values 1 Answer

Saving public variable data set in inspector? 1 Answer

Need help with using Player Prefs to save number of coins collected. 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