- Home /
Question by
Himani_123 · May 29, 2014 at 06:50 AM ·
javascript
how to save and load the game state
i want to save the game state and want to load it on resume button click.But i dont knw how to do that.,i tried the snippet
using UnityEngine;
using System.Collections;
public class SaveState : MonoBehaviour {
// Use this for initialization
void Start () {
Load ();
}
// Update is called once per frame
void Update () {
}
void Load(){
if (PlayerPrefs.GetFloat ("x") != null)
{
transform.position= new Vector3(PlayerPrefs.GetFloat("x"),PlayerPrefs.GetFloat("y"),PlayerPrefs.GetFloat("z"));
}
transform.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
Save ();
}
void Save()
{
PlayerPrefs.SetFloat("x",transform.position.x);
PlayerPrefs.SetFloat("y",transform.position.y);
PlayerPrefs.SetFloat("z",transform.position.z);
}
}
when i attach it to my new game script,after closing when i again click on new game,cubes in my game get displace and also texture i attach to the cubes donot load. why this happen..?? what am i doing wrong ??
Comment
This may sound stupid, but are you calling your save function before you close? And the textures are not loaded because you did not write a method to store the textures of your cubes, you only store its position.