how to save a game

i would like to know how to save a game in the middle of the game and to reload it

It depends on the type of game you are creating, and the type of data you need to save/load.

For simple games, you probably don’t need anything more than PlayerPrefs:

If you search the forums, there are many discussions on PlayerPrefs, as well as more advanced techniques.

are there any video tutorials regarding this

They’re kinda straight forward, what exactly are you trying to save? They’re pretty much good for carrying/saving variables onto the player’s computer through registry files.

saving the game when we are playing

Well you can’t actually make a ‘save’ file. But can store like the player’s position like:

var player : Transform;

function Start () {
DontDestroyOnLoad(this);
}

function Save () {
player = gameObject.FindWithTag("Player").transform;
PlayerPrefs.SetFloat("Player Position X", player.position.x);
PlayerPrefs.SetFloat("Player Position Y", player.position.y);
PlayerPrefs.SetFloat("Player Position Z", player.position.z);
}

function Load () {
var savedPosition = Vector3(PlayerPrefs.GetFloat("Player Position X"), PlayerPrefs.GetFloat("Player Position Y"), PlayerPrefs.GetFloat("Player Position Z"));
Instantiate(player, savedPosition, Quaternion.identity);
}

but this is one untested javascript example.

Inside a script, whenever you want to save, you call

UnityEngine.SaveAll ();

and when you’re ready to load you use

UnityEngine.LoadAll ();

No, not really.

“Saving the game when we are playing” is far too generic.

Saving is tailored to each game individually, and it is your job to choose what/how the data is saved. For large amounts of data you would want to save to a binary, xml, txt, or just about any other type of file. For small amounts of data PlayerPrefs would suffice.

Spend a few hours reading the scripting reference on PlayerPrefs (linked above by geppetto) and if you can’t figure out how to use it from that, use Google to find examples. Or read login4donald’s example.

ok.thank u

With C# at least you can just use standard .NET IO APIs like File.Create, StreamWriter, etc. I’m using it for an iOS/Android game and it works just fine in the editor and on the devices. Player preferences really is just for small, simple preferences (as the name would imply). Once you’re saving game state you really ought to just make a file to save to.

Here’s the shortest (untested) example to give you an idea:

// most types for IO are in System.IO namespace
using System.IO;

// make a path
string path = Path.Combine(Application.persistentDataPath, "MyFile.dat");

// write some data
using (StreamWriter writer = new StreamWriter(File.Create(path)))
{
    writer.WriteLine("Hello, world!");
}

// read it back in
using (StreamReader reader = new StreamReader(File.Open(path, FileMode.Open)))
{
    while (!reader.EndOfStream)
    {
        Debug.Log(reader.ReadLine());
    }
}

But what’s the difference between using the StreamWriter and PlayerPrefs.SetString(). Specifically, what benefits do you gain from using System.IO? I don’t understand, because you can put any kind of data you want inside of a string…