- Home /
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.
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)...
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
Follow this Question
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