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 gogo199432 · Aug 28, 2013 at 09:27 AM · playerprefssave scenegame savestate saving

Save as many pickables as you want to

Hi guys

So I started to play around with the save and load problem in unity. I'm using PlayerPrefs at the moment, and my question is: Is there a way to save as much pickable objects as you want?

So for example I have 5 different keys, which the player can pick up. But since a good script is modular, if I have 15 keys in the next level, I dont want to add another 10 lines into the saving function to save them. I'm thinking about arrays, but have no idea how to do it.

So what I really want to do is, that each key would check when loading up the game, if it was picked up before. If yes, then disable that key, if not then enable it.

Thanks in advance :)

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
0

Answer by oliver-jones · Aug 28, 2013 at 10:34 AM

First of all, I would advise you to check out PlayerPrefsX. It's just like the PlayerPrefs within Unity, but it allows you to save booleans, arrays, vectors, etc ...

Now, I would do something like this, have a save function that you can call, and save a value into it:

 function Save(object : String, value : boolean){
 
     PlayerPrefsX.SetBool(object, value);
 }

Then, whenever your player picks up a key, you can do this:

 Save("key_name", true);

Example:

 //Don't have
 Save("key1", false);
 //Do have
 Save("key2", true);
 //Do have
 Save("key3", true);
 //Don't have
 Save("key4", false);

And if you do want to use arrays:

 SaveArray(KeyIDs[], KeyPickUp[]);
 
 function SaveArray(object : String[], value : boolean[]){
     PlayerPrefsX.SetBoolArray(object, value);
 }


-------- UPDATE --------

Okay, so you have your keys all over your map okay? So what you need to do is reference them in your script when you have picked them up.

So, I'm guessing you have a script that allows you to pick up your key? And I'm guessing your keys have a Unique Object name? So not, then do that first; So all your keys are named like 'Key_1', 'Key_2' ...

Then, within your pick up script, you want to do something like this:

 // we do not have any of the keys
 var key_1 : boolean = false;
 var key_2 : boolean = false;
 var key_3 : boolean = false;

 if(pickedUp.transform.name == "Key_1"){
    key_1 = true;
    Save("key_1", true);
 }
 else if(pickedUp.transform.name == "Key_2"){
        key_2 = true;
        Save("key_2", true);
     }

If you want to load if you have your keys or not:

 function Start(){
    key_1 = PlayerPrefsX.GetBool("Key_1");
    key_2 = PlayerPrefsX.GetBool("Key_2");
    key_3 = PlayerPrefsX.GetBool("Key_3");
 }


---------- UPDATE 2 ----------

Or, if you are comfortable with using arrays, you can do this:

 //Place all your keys from the scene into here (via Inspector)
 var KeyObjects : GameObject[];
 //Make all these false from the inspector
 var KeyHave : boolean[];
 
 function Start(){
 
     KeyHave = PlayerPrefsX.GetBoolArray("KeyHave");
 }
 
 function OnCollisionEnter(hit : collider){
 
     for(var i = 0; i < KeyObjects.length; i++){
 
         if(hit.colldier.transform.name == KeyObjects[i]){
 
             print("We have just walked over key: " + KeyObjects[i]);
             KeyHave[i] = true;
 
             //Saves all the keys value
             Save("KeyHave", KeyHave);
         }
 
     }
 }
 
 
 function Save(name : String, value : boolean[]){
 
     PlayerPrefsX.SetBoolArray(name, value);
 } 
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 gogo199432 · Aug 28, 2013 at 10:48 AM 0
Share

I already checked out PlayerPrefsX but have no clue how to use Arrays, they are so confusing :D

Anyway, does "key_name" add the gameObjects name to it automaticly, so it numbers it like in your example?

And how could I load the saved thing?

avatar image oliver-jones · Aug 28, 2013 at 10:57 AM 0
Share

Updated ^^

avatar image oliver-jones · Aug 28, 2013 at 11:38 AM 0
Share

I haven't tested it - but let me know how it goes

avatar image gogo199432 · Aug 28, 2013 at 04:18 PM 0
Share

$$anonymous$$an, I'm trying really hard to figure out where to place your Array script but with no success. Does it go to the player or on the keycard. Because you are using OnCollisionEnter. And btw I use E to pick up things.

avatar image gogo199432 · Aug 29, 2013 at 08:05 PM 0
Share

Hello? Could someone help me with this?

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

16 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

Related Questions

Unity keeps crashing when I try to save a scene 0 Answers

Save Transform in PlayerPrefs 2 Answers

Saving Player Position Scene to Scene 0 Answers

Web player for my project 0 Answers

How can I save & load gameObjects? 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