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
6
Question by oz m · May 22, 2010 at 04:52 PM · playerprefsserializationsaveload

Save/load playerprefs

I am making a relatively simple game, but i want to know how to save it. I made it using quite a few scripts and i ave two scenes: main menu and the actual game. I want to save the players position and some variables and then be able to have the option to either load them, or make a new file. It would be nice to be able to save up to three or so files. I heard about playerprefs and learned some stuff about it, but i could only find stuff about highscores.


I tried the playerprefs.getint in a script, it didn't work. here's the script i tried it on, before i put in the playerprefs script. It's supposed to respawn an enemy a certain number of times and then stopped. I just wanted, for now, to save how many times it respawned an enemy:

var Beholder : Transform; var targetThing : Transform; var Weapon: Transform; beholder.thing = targetThing; beholder.weapon = Weapon; var deathTimer : int = 0; var maxRespawns : int;

var respawnCount : int = 0.0;

function Update () {

 if(beholder.death)
 {

     deathTimer++;

     if(deathTimer == 50 && respawnCount < maxRespawns)
     {
         deathTimer = 0;
         respawnCount++;
         beholder.death = false;
         var newBeholder = Instantiate(Beholder, transform.position, transform.rotation);
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by user-1846 (google) · May 22, 2010 at 05:32 PM

To save player preferences its actually really simple. They are saved between game sessions and are very easy to use.

Examples (Javascript):

//set a string to a player preference called "MyString"
PlayerPrefs.SetString("MyString", "MyValue");
//now retrieve this value
var test : String = PlayerPrefs.GetString("MyString");
//see? it works with ints too.
PlayerPrefs.SetInt("MyInt", 2);

You can find a complete list of functions by (in unitron) Typing PlayerPrefs, highlighting it, and then hitting the search documentation button.

Hope this helps, Christian Stewart

Comment
Add comment · Show 4 · 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 oz m · May 22, 2010 at 05:39 PM 0
Share

how do i save the player's position and rotation and stuff

avatar image Tetrad · Jun 05, 2010 at 06:30 PM 0
Share

PlayerPrefs only saves very simple types. The easiest way to save the player's position inside PlayerPrefs, for example, is by storing 3 variables for position.x, y, and z.

avatar image user-1846 (google) · Jun 16, 2010 at 02:30 AM 0
Share

OR, you can make a string like position.x+":"+position.y+":"+position.z and then parse it by PlayerPrefs.GetString("position").Split(":");

avatar image bernardfrancois · Mar 11, 2011 at 11:36 AM 2
Share

Note that saving a lot of data to PlayerPrefs can be very slow on Android and iOS devices. To improve this, you can write your own code to save this to a file, or use the following custom PlayerPrefs class we wrote: http://www.previewlabs.com/writing-playerprefs-fast/

avatar image
0

Answer by Utopians · Mar 05, 2016 at 12:52 AM

http://stackoverflow.com/a/21278604

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

1 Person is following this question.

avatar image

Related Questions

Character Positions Not Serializing or Loading Properly? 1 Answer

How can playerprefs save my tower model and its position? 1 Answer

Saving Game Problem 1 Answer

Fast, easy serialization library for Unity? 0 Answers

Unity Not Loading 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