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 Conect11 · Oct 23, 2013 at 04:52 PM · playerprefsstats

[SOLVED] A Playerpref in one place is blocking all my other Playerprefs

So in fixing one issue, I've come across a whole new (potentially bigger) issue. I've been using Playerprefs to save most everything. (Player health, money, items, etc.) Everything was working fine. I came to the new problem of wanting to save enemy status. In other words, when the player killed an enemy, I wanted the enemy to stay dead. (important for boss characters between game sessions) So after several hours over Skype with a very generous fellow Unity fan we had a working script which did exactly what I needed, the enemy does not come back to life after game sessions. HOWEVER, now nothing that would previously save or load does either. It's almost like the new script is blocking any other Playerprefs from saving / loading. Even the debug log doesn't get called anymore. I'm stumped, and am hoping someone might be able to point me in the right direction. Thanks, and God bless. The scripts are below, starting with the Playerprefs master script, which was previously working, then the new script.

SCRIPT !:

 function Start() {
  
 OnLoad();
  
 autoSaveEnable();
  
 }
  
  
  
  
 function autoSaveEnable() {
  
  for(var x = 1; x>0; x++) {
  
 yield WaitForSeconds(5);
  
 Debug.Log("save me");
  
 OnSave();
  
  
 }
  
  
  
 }
  
  
  
  
 // a function created to save a game
  
     function OnSave()
  
     {
  
    
  
  
      
         PlayerPrefs.SetInt("CurXp", Playerhealth.curXp);
         PlayerPrefs.SetInt("MaxXp", Playerhealth.maxXp);
         PlayerPrefs.SetInt("MaxHealth", Playerhealth.maxHealth);
         PlayerPrefs.SetInt("CurHealth", Playerhealth.maxHealth);
         PlayerPrefs.SetInt("Level", Playerhealth.level);
         PlayerPrefs.SetInt("Money", Playermoney.curMoney);
         PlayerPrefs.SetInt("HardTack", Inventory.HardTack);
         PlayerPrefs.SetInt("HealthPotion", Inventory.HealthPotion);
        
  
  
  
        
  
      
  
     }
  
    
  
    
  
    
  
    
  
     // a function created to load a game
  
     function OnLoad()
  
     {
  
    
  
         Debug.Log("loaded");
  
  
  
        Playerhealth.curXp = PlayerPrefs.GetInt("CurXp");
        Playerhealth.maxXp = PlayerPrefs.GetInt("MaxXp");
        Playerhealth.maxHealth = PlayerPrefs.GetInt("MaxHealth");
        Playerhealth.curHealth = PlayerPrefs.GetInt("CurHealth");
        Playerhealth.level = PlayerPrefs.GetInt("Level");
        Playermoney.curMoney = PlayerPrefs.GetInt("Money");
        Inventory.HardTack = PlayerPrefs.GetInt("HardTack");
        Inventory.HealthPotion = PlayerPrefs.GetInt("HealthPotion");
       
        
  
        
  
     }


SCRIPT 2

 static var GuardianHealth : int = 5;
  
 function Start(){
  if(PlayerPrefs.GetInt("GuardianDead", 0) == 1){
   //object.active = true;
   //object.enabled = true;
   Destroy(gameObject);
 }
 }
 
      
 
     function OnTriggerEnter (other : Collider){
     if (other.gameObject.name == "sword") {
     GuardianHealth = -10;
     }
     else 
     {
     if (other.gameObject.name == "club")
     {
         GuardianHealth = -1;
     
 }
 
 
  
 if(GuardianHealth <= 0 )
 {
 GuardianHealth = 0;
 audio.Play();
 PlayerPrefs.SetInt("GuardianDead", 1);
 Destroy(gameObject);
 }
 }
 }


Comment
Add comment · Show 2
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 robertbu · Oct 23, 2013 at 05:04 PM 0
Share

I'm fuzzy about your issue. What I see right now is that all the code in the enemy health script is executed in OnTriggerEnter(). At the (re)start of a level, this is not executed. I think you need to make all this code its own function and execute it both in OnTriggerEnter() and in Start().

avatar image Conect11 · Oct 23, 2013 at 05:24 PM 0
Share

I briefly thought that last night. Will try it out when I get home.

1 Reply

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

Answer by Conect11 · Oct 24, 2013 at 12:29 AM

Go figure. Another script WAS blocking the other Playerprefs, but not the script I thought. I had forgotten about my entirely separate EXP script, which gets called on destroy. Unfortunately, since it did, the command never fired to save anything else to Playerprefs. Simply eliminating that script, and placing its contents into the enemy health script solved my issue.

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

14 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

Related Questions

Attempting to store an Inventory Array in PlayerPrefsX 1 Answer

PlayerPrefs saving, but not loading some values 2 Answers

Player Prefs adding XP randomly 2 Answers

Need help with PlayerPrefs 2 Answers

Grabbing PlayerPrefs variables from another game. 0 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