Save Game setup

I’m trying to finish my first Unity game, for the iphone. I need to save out the game and have it load where you last played.

I understand that it is through playerprefs that I set this up however I don’t know the best way to set this up and I’m looking for examples. I’m sorry but I am new to this.

If anyone can suggest, should I use a function one for saving the game and one for loading and how can I have these accessed when the game is exited automatically, and read when the game is started again.

thanks for suggestions.

Hi, welcome to the forum!

What kind of data do you need to save? Will you need to save data for more than one player on the same device?

This is just a one player game.

I need to save information like what level they are on, perhaps how much time they have left to play and how many lives they have.

Thankyou for any ideas.

I need the same thing!! Please help! I am also almost finished my first iPhone game. Lolz. What’s yours about?

I’m working on that in my game too. Most of my code is set up to save everything i need in the player prefs, but im having trouble getting the playerpref functions to work right. Could anyone explain in some detail how the playerprefs work exactly?

I’m going to try this tonight, I was hoping for some more direction before I begin but I think I’ll start with a function that gets checked by LateUpdate or Start to see if the game has ended.

Of course this is the question. It should get activated when the game is interrupted not when it’s over, so how to make it activate and save the information is what I’m puzzling.

This works for me to store the level the player has reached and recall it later when the app is relaunched (as part of a Global script which is not destroyed upon loading a new scene):

function Start () {

if (PlayerPrefs.GetInt(“LevelsComplete”) > Global.LevelsComplete) Global.LevelsComplete = PlayerPrefs.GetInt(“LevelsComplete”);

}

… where Global.LevelsComplete increments after finishing a new level.

Merry Christmas

using UnityEngine;
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


public static class Persist
{
    public static void serializeObjectToFile( object obj, string filename )
    {
        // Get the path to the file to save to
        string path = Persist.pathForDocumentsFile( filename );

        // Create a StreamWriter to write the XML to disk
        StreamWriter sw = new StreamWriter( path );
        XmlSerializer serializer = new XmlSerializer( obj.GetType() );
		serializer.Serialize( sw, obj );
        sw.Close();
    }
	
	
    public static object deserializeObjectFromFile( string filename, Type type )
    {
        // Get the path to the file to save to
        string path = Persist.pathForDocumentsFile( filename );
        object obj;

        StreamReader sr = new StreamReader( path );
        XmlSerializer serializer = new XmlSerializer( type );
        obj = serializer.Deserialize( sr );
		sr.Close();

        return obj;
    }
	
	
	public static void writeStringToFile( string str, string filename )
	{
        // Get the path to the file to save to
        string path = Persist.pathForDocumentsFile( filename );
		
        StreamWriter sw = new StreamWriter( path );
        sw.Write( str );
        sw.Close();
	}
	
	
	public static string readStringFromFile( string filename )
	{
        // Get the path to the file to save to
        string path = Persist.pathForDocumentsFile( filename );
		
        StreamReader sr = new StreamReader( path );
        string str = sr.ReadToEnd();
        sr.Close();
		
		return str;
	}
	
	
    public static string pathForDocumentsFile( string filename ) 
    { 
        // Application.dataPath returns
        // /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/myappname.app/Data
#if !UNITY_IPHONE 
		string path = Path.Combine( Application.dataPath, "Documents" );
		Debug.Log( path );
#else
        string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
#endif
		
        // Strip application name 
        path = path.Substring( 0, path.LastIndexOf( '/' ) );
		
        return Path.Combine( Path.Combine( path, "Documents" ), filename );
    }


}

And a happy new year :lol: , good to see you here too :wink:

Wow! Mr Eskema himself on the Unity forum. You’re first couple tutorial videos were excellent. Keep up the good work.

This is great, the best of the Unity Forum, which is by far the best forum for software users and game enthusiasts alike!

thank you!

This may sound silly, but how do I go about using this code. I too am trying to work out how to save a game. I just wanted a simple save and continue system for my iphone game.

Jfrost

Does the player prefs work? I would like to use this code, but I don’t know how