I want a script that if u hit back to menu its saves your level and when u hit start it comes back to that level, i should ‘‘remember’’ wich level you was on if u close the game and start it again! Is there some1 that can help me with that. My game is a ‘‘platform’’ game isch…
to save game data after the game is off you would need to consider where you would save to.
if your game is intended for a webplayer access to a hard drive is limited by a firewall. in some cases you would need to have a server setup for game copys to send /retrieve data. if you are building for PC then you can write to a hard drive.
Unity has no built in function for saving progress. you whould need to come up with your own system through code that would get nessessary info about your game progress and put it into a either a byte array format or string format to save. you would obviosly need a code to read what you saved on startup and make sence of it.
anyways here is a code example i wrote if you are building for PC.
put this on any game object you want to return to saved position. push the “p” button to save.
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Diagnostics;
public class donut : MonoBehaviour {
float f;
Vector3 vec;
String txt;
public float myhealth;
public String[] arr;
void Start(){
if(File.Exists("C:/somefolder/"+transform.name+".txt")){
txt=File.ReadAllText("C:/somefolder/"+transform.name+".txt");
print("got this: "+txt);
//split the string into an array of three strings at the seporator we wrote in our file
arr=txt.Split("!"[0]);
// now change each of the strings back to numbers
float.TryParse(arr[0],out vec.x);
float.TryParse(arr[1],out vec.y);
float.TryParse(arr[2],out vec.z);
float.TryParse(arr[3],out myhealth);
//move our character to the position we got from our file
print ("goto:"+vec);
transform.position=vec;
}
}
void Update(){
// push the p button on your keyboard to save position
if(Input.GetKeyDown("p")){
print ("saving position"+transform.position);
txt=transform.position.x+"!"+transform.position.y+"!"+transform.position.z;
txt=txt+"!"+myhealth;
if(!Directory.Exists("C:/somefolder")){Directory.CreateDirectory("C:/somefolder");}
File.WriteAllText("C:/somefolder/"+transform.name+".txt",txt);}
}
}