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 thatanimator · Feb 05, 2013 at 05:11 AM · playerprefsloadingsavingbooleans

PlayerPrefs logic and bool?

Hello! This is yet another PlayerPrefs related question, but I'm not here to ask you to write any code for me, just want to ask you guys to clarify the thinking process behind this function a bit since the Script Reference is pathetic.

I have a script with a ton of booleans that unlock levels in my game. I want to save these booleans when the player either OnApplicationPause's the game or on certain levels of the game.

An example would be:

 static var tutorialUnlocked = true;

How would I go about saving that boolean? http://wiki.unity3d.com/index.php?title=BoolPrefs , I don't mind using this script, but since I don't even understand the logic behind PlayerPrefs I don't really know where to even begin.

Currently I have a script that has a function which unlocks levels in the game. The function gets called at the end of a short cutscene-ish animation at the end of each level, for example:

 if(Application.loadedLevel == 12 && lifeManager.life > 2 && ProgressScript.caveUnlocked == false){
     ProgressScript.caveUnlocked = true;

Now I wouldn't mind adding a PlayerPrefs in that function, I just don't understand the logic. Would I instead of saying "ProgressScript.caveUnlocked = true;" just say "PlayerPrefsX.SetBool(ProgressScript.caveUnlocked = true);"? (granted I'd be using the above mentioned work around) So instead of saving the state of a script, I am actually setting the boolean using the PlayerPrefs? Testing it, it doesn't work..

And then of course, I would like to load those prefs at the start of the game again (I have a blank scene at 0 for this - main menu is at 1), assuming I'd understand the logic behind saving, loading shouldn't be too far away.

If anyone could at least shed some insight in to how I'd go about saving the state of a script/gameobject's booleans I'd be able to sleep tonight, as that is the final piece of my little iPhone puzzle atm.

Thank you very much!

PS. I can't believe how uninformative the script reference is about this

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

1 Reply

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

Answer by Lukas H · Feb 05, 2013 at 08:32 AM

PlayerPrefs are not meant to be variables to hold your current game state, instead it is a very easy way to persist data between game starts.

Suppose we can unlock levels, I would create a LevelManager script which keeps track of all the level states. It would load the level states on start up and save the level state everytime I unlock a level. The LevelManager would look something like below (C#)

 using UnityEngine;
 using System.Collections;
 
 public class LevelManager : MonoBehaviour
 {
     private bool level1Unlocked = false;
 
     // Use this for initialization
     void Start()
     {
         int state = PlayerPrefs.GetInt("Level1State", 0);   //0 is default value, when "Level1State" has never been saved before 
 
         if (state == 1)
             level1Unlocked = true;
         else
             level1Unlocked = false;
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
 
     public bool IsLevel1Unlocked()
     {
         return level1Unlocked;
     }
 
     public void UnlockLevel1()
     {
         level1Unlocked = true;
 
         PlayerPrefs.SetInt("Level1State", 1);
     }
 }
Comment
Add comment · Show 7 · 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 thatanimator · Feb 05, 2013 at 11:57 PM 0
Share

Thank you for the quick reply! It being in C# is of course confusing me a bit, but I've tried to rework your script to JS.

I have a ProgressScript.js which will function as my level manager, so what I did is ins$$anonymous$$d of having all the levels lined up as static bools, I tried:

 var foreststate = PlayerPrefs.GetInt("foreststate", 0);
     if (foreststate == 1)
             forestUnlocked = true;
         else
             forestUnlocked = false;
     
 }

I put that in a function Start(); function, and I assume that I would then PlayerPrefs.SetInt("foresstate", 1); in whatever scripts I use for unlocking my levels ins$$anonymous$$d of just calling "forestUnlocked = true".¨ Unity has a problem with:

var foreststate = PlayerPrefs.GetInt("foreststate", 0);

though, the ever occuring ";" error..

I will try to hack around a bit more.. I guess I have a bit more understanding of this, not to the extent where I can complete the save feature though :( hack hack

avatar image thatanimator · Feb 06, 2013 at 05:05 AM 0
Share

I think I get a bit of the way you're supposed to think with this. I've set up PlayerPrefs.GetInt("Level1State", 0); like you mentioned, and then an update function which checks the integer to see if it's 1 or 0.

I then set the int to 1 when unlocking a level.

It doesn't follow through to the main menu though, I assume since it isn't a static variable any more? I thought the whole PlayerPrefs saved this data?

If I GetInt("levelname", 0); every time, will it 0-out the int every time? why do I want to Get 0 if I've set it to 1? sounds like a non-static variable to me?

avatar image thatanimator · Feb 11, 2013 at 07:54 AM 0
Share

ok I guess I understand the way I should work using integers to set a boolean to true or false, easy enough. however, unity is giving me lip about one thing now.

I have one script that unlocks the levels:

 var life$$anonymous$$anager : life$$anonymous$$anager;
 var ProgressScript : ProgressScript;
 
 function Update () {
 if(Application.loadedLevel == 12 && life$$anonymous$$anager.life > 2){
     ProgressScript.PlayerPrefs.SetInt("cavestate", 1);    
 }

and one script that stores the progress/unlocked levels:

 function Start(){
 cavestate = PlayerPrefs.GetInt("cavestate");
 }
 
 public var cavestate = 0;
 public var caveUnlocked = false;
 
 function Update(){
     if (cavestate == 1){
             caveUnlocked = true;
             }
         else
             caveUnlocked = false;
 }

except for the redundant use of variables, this works. IF I use:

 ProgressScript.forestUnlocked = true;

which is the way I've done it up until now, and it works. if I use the SetInt, which I want to use to actually SAVE progress, I get this error in the console:

 NullReferenceException: Object reference not set to an instance of an object
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
 UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
 ProgressScore.Update () (at Assets/Scripts/Points/ProgressScore.js:18)

which of course is silly, because the script works totally fine, except for when I want to use SetInt rather than just turning a boolean into True. the Null Esxception is also bull because I've set the object that has the attatched script to it in the inspector, and calling Debug.Log(life$$anonymous$$anager"); tells me that there is an object correctly assigned in the inspector:

 healthbar (life$$anonymous$$anager)
 UnityEngine.Debug:Log(Object)
 ProgressScore:Update() (at Assets/Scripts/Points/ProgressScore.js:18)



so the script doesn't set any integers because it doesn't go through with the if-statement, because it can't find the script..that it can find.

avatar image thatanimator · Feb 11, 2013 at 10:00 AM 0
Share

ProgressScript.PlayerPrefs.SetInt("cavestate", 1);

Removing the ProgressScript part worked..

The previous help was not the correct answer, but it helped me along the way to figuring it out, thank you very much!

avatar image Lukas H · Feb 11, 2013 at 10:02 AM 0
Share

Glad it helped. Sorry I didn't saw your follow-up questions earlier!

Show more comments

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

12 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

Related Questions

Saving and load from player prefs in Unity3D on mobile devices?? 0 Answers

Problems with Playerprefs script 1 Answer

Saving Scenes and loading GameObjects? 4 Answers

How to efficiently hand out rewards based on the level that the player completed? 3 Answers

Saving & Loading the scene (or at least one array) via Javascript 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