- Home /
Creating a Save/Load game
Okay so what I want to do is this.
First I want to be able to save any changes that my player makes with any object in the scene. For example, say he picks up an object, then moves it across the room, and then drops it, leaves the scene, then returns later. I'd like the object to be in the last place he left it. The same with other things like destructibles, enemies, etc.
I figure that the best way to do this would be through a temporary file that is saved while the player is playing. I'm not sure how to create this though, and I'm also not sure how to make said file, and am having trouble finding/understanding the documentation. (Would PlayerPrefs be able to do what I'm asking?)
Then also I would like him to be able to save all the changes into a permanent save file which he can later load, and pick up where he left off.
Thank you for all your help!
Answer by AFDozerman · Mar 24, 2014 at 12:36 AM
What language are you using? This is how to create a file in c#:
http://stackoverflow.com/questions/802541/creating-an-empty-file-in-c-sharp
It essentially boils down to
String path = Directory.GetCurrentDirectory();
String OP = "Sending this to file";
Using(System.IO.File.Create("textfile.txt"));
System.IO.File.WriteAllText(textfile.txt,OP);
Thanks, this was very helpful. For those that are interested, this is the basic script I was able to come up with:
//This is really cluttered, and not the best, but it works as an excellent
//example for anyone that wants to build a basic save game feature
#pragma implicit
import System;
import System.Runtime.Serialization.Formatters.Binary;
import System.IO;
//If set to true it will save the transform once exactly
var save : boolean;
//Get a binary formatter
var serializer = BinaryFormatter();
//Create an in memory stream
var m = $$anonymous$$emoryStream();
function Start ()
{
//Checking for our save game
if(File.Exists(Application.persistentDataPath + "/highscores.dat"))
{
//Opening the file
var f = File.Open(Application.persistentDataPath + "/highscores.dat", File$$anonymous$$ode.Open);
//Restoring the class we serialized
var loader = serializer.Deserialize(f);
//setting the values.
this.transform.position.x = loader.transformX;
this.transform.position.y = loader.transformY;
this.transform.position.z = loader.transformZ;
}
}
function Update ()
{
if(save == true)
{
//Creating our file to save to
var f = File.Create(Application.persistentDataPath + "/highscores.dat");
//The class we're going to use to store our data
var saveThing = new SaveData();
//Setting the values
saveThing.transformX = this.transform.position.x;
saveThing.transformY = this.transform.position.y;
saveThing.transformZ = this.transform.position.z;
//Serialization of our class
serializer.Serialize(f,saveThing);
//ending our save
save = false;
}
}
//A class to store our data
class SaveData extends System.Object
{
var transformX;
var transformY;
var transformZ;
}`
Answer by xt-xylophone · Mar 24, 2014 at 01:26 AM
This tutorial explained everything for me. Highly recommend!
Your answer
![](https://koobas.hobune.stream/wayback/20220613140107im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loading and saving default files on Android 1 Answer
Saving your scene and location in game? 1 Answer
How to load player last scene visited 1 Answer
Scene Saving 1 Answer
script to save and load a game 3 Answers