- Home /
Transform object issues
Hello guys, I've encountered an issue I can't figure out; How to move a player to their last saved location upon load. I have three scripts written in C# whose purpose is to accomplish this:
Pause, a script to perform a save/load function
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Pause : MonoBehaviour {
public static List<Game> savedGames = new List<Game>();
public static bool paused = false;
public Texture saveButton;
public Texture loadButton;
public static bool saving = false;
void Update(){
if(Input.GetKeyUp (KeyCode.P)){
paused = !paused;
}
}
void OnGUI(){
if(paused && GUI.Button(new Rect(525, 250, 256, 64), saveButton, GUIStyle.none)){
Pause.savedGames.Add(Game.current);//Add a save file
BinaryFormatter bf = new BinaryFormatter();//Prepare serialization tool
FileStream file = File.Create ("C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd");//Defining save file path, and name the save file there
bf.Serialize(file, Pause.savedGames);//Use the prepared binary formatter to serialize the designated classes
file.Close();//Close the file (duh)
Debug.Log ("C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd");
saving = true;
}
if(paused && GUI.Button(new Rect(525, 350, 256, 64), loadButton, GUIStyle.none)){
if (File.Exists ("C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd")) {//Make sure there is a saved game
BinaryFormatter bf = new BinaryFormatter ();//Prepare deserialization tool
FileStream file = File.Open ("C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd", FileMode.Open);//Find file
Pause.savedGames = (List<Game>)bf.Deserialize (file);//Use deserialization tool
file.Close ();//Close the file (duh)
Debug.Log ("Loaded file C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd");
Player.SetLoc ();
} else {
Debug.Log ("No save file found");
}
}
}
}
Game, a script to hold variables that will be saved/loaded:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Game{
public static Game current;
public static Vector3 pLoc;
/*public static float xLoc;
public static float yLoc;
public static float zLoc;*/
void Update(){
if(Pause.saving){
/*xLoc = Player.ReportXLoc();
yLoc = Player.ReportYLoc();
zLoc = Player.ReportZLoc();*/
pLoc = Player.GetLoc();
Pause.saving = false;
}
}
}
And finally player, which is attached to(you guessed it) the player.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public static GameObject player;
public static float xLoc;
public static float yLoc;
public static float zLoc;
public static Vector3 playerLoc;
// Use this for initialization
void Start () {
player = GameObject.Find ("First Person Controller");
}
public static Vector3 GetLoc(){
playerLoc = player.transform.position;
return playerLoc;
}
public static void SetLoc(){
player = GameObject.Find ("First Person Controller");
player.transform.position = new Vector3 (Game.pLoc.x, Game.pLoc.y, Game.pLoc.z);
}
}
I am thoroughly stumped, any help would be greatly appreciated, thanks!
Answer by SenorGoatMan · Aug 07, 2014 at 02:17 AM
Well, sir, it is plain to see for even the blindest of fools that you must serialize the x, y and z values of you player's location upon saving and then deserialize them upon loading.
Stop talking to yourself on a public forum, people might think we're weird
Your answer
![](https://koobas.hobune.stream/wayback/20220613160019im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Picking up an Object and locking its position relative to parent. 1 Answer
Javascript to C# 1 Answer
Moving an object away from collider 1 Answer
GUI.Button child of the camera . 0 Answers
How do I serialize a class? 2 Answers