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
2
Question by oliver-jones · Mar 16, 2011 at 10:00 PM · variablesbooleanloadingsaving

Save & Loading Variables

Hello,

I want to have the ability to save my game variables when I leave the game. I'm not worried about positions of objects or anything like that. I literally want to grab all the variables I have in my scripts, and save them as an XML or something so next time, when I open up my game all my variables will be replaced with the saved ones.

I've tried messing around with a Save Load script, but I just can't get it to work. So like I said, is there a way to pull all my current game variables, and save them - and then load them back up?

Would appreciate any help or info.

Thanks!

-- EDIT --

So, Ive been messing around with the PlayerPrefs, and im not 100% sure on how it works. This is how I have my setup.

-- EDIT Update --

So i have this to save the current variables (well 1 at this current time):

if (GUI.Button(Rect(10,70,50,30),"Save")){
        PlayerPrefsX.SetBool("inventoryKnife", StoryControl.inventoryKnife);
    }

On my landing scene (0) - how would I go about loading the variables up into the game?

function Start(){
     Application.LoadLevel(PlayerPrefs.GetInt("inventoryKnife")); //??
    //or
     Application.LoadLevel(PlayerPrefsX.GetBool ... //?
}

I have no idea how PlayerPrefs work.

Comment
Add comment · Show 1
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 oliver-jones · Mar 16, 2011 at 11:01 PM 0
Share

Another thing I need to mention is that the variables I want to save are all booleans ... and I have hundreds of them!

2 Replies

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

Answer by Statement · Mar 16, 2011 at 11:34 PM

Are you sure you aren't supposed to call SetInt? I am under the impression you want to set the saved level on Save. And you can provide a default value to GetInt if it doesn't exist so you don't have to check if the key exists. It will also get rid of your static variable.

function Start()
{
    // If no savedLevel was found it will return 1 as default.
    Application.LoadLevel(PlayerPrefs.GetInt("savedLevel", 1));
}

And if you want to set the savedLevel to something, for instance set it to level 2:

if (GUI.Button(Rect(10,70,50,30), "Save"))
{
    PlayerPrefs.SetInt("savedLevel", 2);
}

PlayerPrefs is kind of easy. You can think of it as having static variables that are saved between games. There is a Get and a Set for Int, Float and String. For ease, GetInt etc accept an optional parameter that is the default value if no key was found.

See also BoolPrefs and ArrayPrefs.

Given you have a script you want to save/load savegame data to/from:

// The data we want to save. var inventoryKnife : boolean;

function OnEnable() { Load(); }

function OnDisable() { Save(); }

function Load() { inventoryKnife = PlayerPrefsX.GetBool("inventoryKnife", false); }

function Save() { PlayerPrefsX.SetBool("inventoryKnife", inventoryKnife); }

Comment
Add comment · Show 6 · 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 oliver-jones · Mar 16, 2011 at 11:39 PM 0
Share

Okay - so what about booleans? Example: my inventory, I have items that are laid out like so: inventory$$anonymous$$nife = false || inventoryPaper = true ... how can I save those values

avatar image Statement · Mar 17, 2011 at 12:10 AM 0
Share

I updated my answer with links to scripts on the wiki. If you want to you can encode the booleans as bitflags in an integer. That way you can use 1 int for 32 booleans ins$$anonymous$$d of using 1 int for 1 boolean as BoolPrefs do.

avatar image oliver-jones · Mar 17, 2011 at 12:30 AM 0
Share

Thanks - please read my updated question, im struggling to load

avatar image Statement · Mar 17, 2011 at 12:46 AM 0
Share

It seems you're confused that you need to pass anything you get from PlayerPrefs to LoadLevel. This is not the case. We call upon LoadLevel with the value of the level in the first example. We don't want to pass the value of the inventory$$anonymous$$nife to LoadLevel, it doesn't make any sense at all. PlayerPrefs is just storing data that you can get or set. Nothing more than that. See my updated answer. Application.LoadLevel(PlayerPrefs.GetInt("inventory$$anonymous$$nife")); doesn't make any sense at all, it would read "load the level + do we have a knife?". Dont use LoadLevel on inventory$$anonymous$$nife :)

avatar image Statement · Mar 17, 2011 at 12:10 PM 0
Share

If you're still having problems with player prefs - I spent the entire night yesterday writing a simpler set of tools you might like. But I am in a hurry to go on a small holiday so I will have to post back to you earliest monday.

Show more comments
avatar image
0

Answer by raul corrales · Mar 16, 2011 at 10:25 PM

hi, you can use PLAYERPREFS.

Documentation/Documentation/ScriptReference/PlayerPrefs.html

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 oliver-jones · Mar 16, 2011 at 10:51 PM 0
Share

Okay,thanks - could you read my updated question. Thanks!

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

No one has followed this question yet.

Related Questions

PlayerPrefs not saving mission info at all. 0 Answers

Problem about reading and writing from text files 1 Answer

Best way to load the position of my player? 1 Answer

Saving Scenes and loading GameObjects? 4 Answers

SerializationException: serializationStream supports seeking, but its length is 0 when trying to load a save game 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