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
1
Question by Mattchuuu · Apr 26, 2012 at 11:33 PM · savemultiple

Having multiple save files

Hey there, I was wondering if there was an easy way to have multiple characters with their own save slot. I tried this and was able to create 4 slots, each saved their own playerprefs with a name ("Slot1-Name: Steve"), and this worked alright at the beginning, but later On I found that if I try to use any variable that another save slot has stored it will also change that slave slot as well. With this method it looks like I'll just have to create the same game 4 times over just for it work.

I'm somewhat willing to do that but it just seems so inefficient, is there any other way to do 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
1

Answer by aldonaletto · Apr 27, 2012 at 01:02 AM

You can save the variables prefixed with the slot or user name - for instance:

 PlayerPrefs.SetFloat(userName+":Health", health);
 PlayerPrefs.SetInt(userName+":HasRifle", hasRifle?1:0);
 PlayerPrefs.SetInt(userName+":CurLevel", Application.loadedLevel);

To keep track of the users, you could save a NumUsers value and the user names as User1, User2 etc. - read NumUsers first, allocate the user names array and load them:

var numUsers: int; var userNames: String[];

function Start(){ numUsers = PlayerPrefs.GetInt("NumUsers", 0); userNames = new String[numUsers]; for (var i = 0; i < numUsers; i++){ userNames[i] = PlayerPrefs.GetString("User"+i.ToString()); } } When registering a new user, save it using the current numUsers as the index, increment numUsers and save it too:

  PlayerPrefs.SetString("User"+numUsers.ToString(), newUserName);
  numUsers++;
  PlayerPrefs.SetInt("NumUsers", numUsers);
  // update the PlayerPrefs file to avoid data loss case the game or computer crashes
  PlayerPrefs.Save();
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

[SOLVED] Save System with Serialize multiple objects 2 Answers

How to save multiple instances of the same class 3 Answers

Saving with PlayerPrefs and having multiple character slots? 5 Answers

How do I save a custom class of variables to playerprefs? 2 Answers

SimpleJSON array problem 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