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 JLSteward · Sep 30, 2012 at 03:23 AM · playerprefssaveload

Saving and Loading data in one scene

I have read a few very helpful articles on using PlayerPrefs to save information. My question (or questions) regards using it for a temporary save when you are leaving an area in a game to return later. For instance, my character puts a book down on a table. They then leave the scene and go into another one. I want my character to enter the previous scene and the book be exactly where it was left. Now, I'm sure PlayerPrefs can do this, but how?

The scene will possibly have a great many items change where they were, or even new items put down. Will each individual gameobject need a script using PlayerPrefs if I place/move one? I'm fairly new to scripting, so any help would be appreciated.

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
Best Answer

Answer by Seth-Bergman · Sep 30, 2012 at 05:07 AM

OK, first off, you really should only need to use PlayerPrefs for saving data BETWEEN GAME SESSIONS. So if you want the book, for example, to be in the exact same place the next time you start the program, it would make sense to use PlayerPrefs, but otherwise, just a normal variable is all you would need. (the only difference with a PlayerPref var is that it can be saved to the disk)

That being said, for saving data between scenes, as you've described, you would simply make the relevant variable(s) static, so that they retain their values between scenes. This WOULD require you to use a unique var for each object, essentially.

For storing the position of an object, you would want to save the transform.position, which is a Vector3, and also the transform.rotation, which is a Quaternion, SO, here's a simple example:

(script on the book)

 static var book1Pos : Vector3 = Vector3(0,0,0);// set initial position here
 static var book1Rot : Quaternion;
 
 function Awake(){
 transform.position = book1Pos;
 transform.rotation = book1Rot;
 }
 
 function LeaveScene(){ // to be run before changing scene
 book1Pos = transform.position;
 book1Rot = transform.rotation;
 }

Of course, if you have lots of objects to keep track of, it would make more sense to use one controlling script to keep track of all objects..

 class SceneObject {     
 myRotation : Quaternion;
 myPosition : Vector3;
 myObject : GameObject;

 function SetObject(){
 myObject.position = myPosition;
 myObject.rotation = myRotation;
 }
 function SetPosition(){
 myPosition = myObject.position;
 myRotation = myObject.rotation;
 }
 }

 static var book1 : SceneObject;
 static var book2 : SceneObject;
 
 function Awake(){
 book1.SetObject();
 book2.SetObject();
 }
 
 function LeaveScene(){ // to be run before changing scene
 book1.SetPosition();
 book2.SetPosition();
 }

something like this could work.. but keep in mind this is not a complete example, as you would still need to initialize the instances somehow(book1, book2)...

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 JLSteward · Sep 30, 2012 at 05:32 AM 0
Share

Thanks for the reply and the clarifications. I forgot about variables! Very important to remember... I'll have to work up to this, but I was able to understand where you're going with this. 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

11 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

Related Questions

C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 1 Answer

PlayerPrefs 2 Answers

There are wrong mathematical equations in PlayerPrefs. How can I resolve it? 1 Answer

Highscore won't save 3 Answers

how to save a load a 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