Save and Load Game

Hi guys.I want to ask something about save and load game. In my game there are two level and I did scene that you can choose level. And I want to if you don’t complete the first level , you can’t choose second level. And you complete the first level the game save outomaticly and you can choose second level another time in scene. How can I do? Thank you in advance. (If you can, please javascript)

You will need to learn about a specific function, known as UTF8Encoding();

This isn’t exactly what you want, but this IS a load/save script.

import System;
import System.Collections;
import System.Xml;
import System.Xml.Serialization;
import System.IO;
import System.Text;

class TempData
{
    var x : float;
    var y : float;
    var z : float;
    var name : String;
}

class UsernameData
{
   public var _iUser : TempData = new TempData();
   function UsernameData() { }
}
private var _Save : Rect;
private var _Load : Rect;
private var _SaveMSG : Rect;
private var _LoadMSG : Rect;
private var _FileLocation : String;
private var _FileName : String = "SaveData.xml";

var _Player : GameObject;
var _PlayerName : String = "Joe Schmoe";

private var myData : UsernameData;
private var _data : String;

private var VPosition : Vector3;

function Awake () { 
      _FileLocation=Application.dataPath;
      myData=new UsernameData();
}

function UTF8ByteArrayToString(characters : byte[] )
{     
   var encoding : UTF8Encoding  = new UTF8Encoding();
   var constructedString : String  = encoding.GetString(characters);
   return (constructedString);
}

function StringToUTF8ByteArray(pXmlString : String)
{
   var encoding : UTF8Encoding  = new UTF8Encoding();
   var byteArray : byte[]  = encoding.GetBytes(pXmlString);
   return byteArray;
}
function SerializeObject(pObject : Object)
{
   var XmlizedString : String  = null;
   var memoryStream : MemoryStream  = new MemoryStream();
   var xs : XmlSerializer = new XmlSerializer(typeof(UsernameData));
   var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
   xs.Serialize(xmlTextWriter, pObject);
   memoryStream = xmlTextWriter.BaseStream;
   XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
   return XmlizedString;
}
function DeserializeObject(pXmlizedString : String)   
{
   var xs : XmlSerializer  = new XmlSerializer(typeof(UsernameData));
   var memoryStream : MemoryStream  = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
   var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
   return xs.Deserialize(memoryStream);
}
function CreateXML()
{
   var writer : StreamWriter;
   var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
   if(!t.Exists)
   {
      writer = t.CreateText();
   }
   else
   {
      t.Delete();
      writer = t.CreateText();
   }
   writer.Write(_data);
   writer.Close();
   Debug.Log("File written.");
}
   
function LoadXML()
{
   var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
   var _info : String = r.ReadToEnd();
   r.Close();
   _data=_info;
   Debug.Log("File Read");
}

Best of luck.

Based on your specific use case and the comments to the question, I would like to suggest PlayerPrefs.

When the user completes the level, you save the highest unlocked level, like this:

PlayerPrefs.SetInt("HighestLevel", 2);

When you load the game, you check what is the highest unlocked level, like this:

HighestLevel = PlayerPrefs.GetInt("HighestLevel", 1);

The second argument is optional, to say what is the default value.

Finally, in your screen where the player can choose a level to play, you check against this HighestLevel variable to see which buttons you need to display.

In the same way of course, you can store the score instead of (or in addition to-) the highest level.

Lastly, here is a quick link to the PlayerPrefs documentation

Let me know if something is not clear.