i created a script that allow you to save player position and rotation, using the Instantiate instance to create a temporary spawn, so if you load the game, player have to start on the temporary spawn.
Anyway the scripts are these:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.SceneManagement;
public class SaveSystem : MonoBehaviour {
GameObject player;
GameObject cam;
GameObject start;
Start_Scene startScript;
private Scene scene;
void Start()
{
DontDestroyOnLoad (this);
if (FindObjectsOfType (GetType ()).Length > 1)
{
Destroy (gameObject);
}
player = GameObject.Find ("PlayerController");
cam = GameObject.Find ("Camera");
start = GameObject.Find ("Start Scene");
startScript = start.GetComponent<Start_Scene> ();
}
public void SaveData ()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/Data.dat");
PlayerData data = new PlayerData ();
scene = SceneManager.GetActiveScene ();
data.sceneName = scene.name;
data.posx = player.transform.position.x;
data.posy = player.transform.position.y;
data.posz = player.transform.position.z;
data.rotx = cam.transform.localRotation.x;
data.roty = cam.transform.localRotation.y;
data.rotz = cam.transform.localRotation.z;
data.rotw = cam.transform.localRotation.w;
bf.Serialize (file, data);
file.Close ();
}
public void LoadData()
{
if(File.Exists (Application.persistentDataPath + "/Data.dat"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/Data.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
PlayerPrefs.DeleteAll ();
SceneManager.LoadScene (data.sceneName, LoadSceneMode.Single);
Vector3 newPlayerPos = new Vector3 (data.posx, data.posy, data.posz);
Quaternion newPlayerRot = new Quaternion (0, data.roty, 0, data.rotw);
startScript.LoadDataState (newPlayerPos, newPlayerRot);
}
}
}
[Serializable]
class PlayerData
{
public string sceneName;
public float posx;
public float posy;
public float posz;
public float rotx;
public float roty;
public float rotz;
public float rotw;
}
using UnityEngine;
using System.Collections;
public class Start_Scene : MonoBehaviour {
GameObject cam;
GameObject lockCam;
GameObject player;
GameObject spawn;
GameObject sceneMan;
public GameObject dynamicSpawn;
public string spawnState = "Default";
private CameraMouseLook camScript;
private Lock lockScript;
private PlayerMovement playScript;
private Scene_Manager sceneScript;
private IEnumerator camRotationRoutine;
private IEnumerator camRotationRoutine2;
private float delay = 0.25F;
private Vector3 playPos;
private Quaternion playRot;
void Start ()
{
DontDestroyOnLoad (this);
if (FindObjectsOfType (GetType ()).Length > 1)
{
Destroy (gameObject);
}
spawnState = PlayerPrefs.GetString ("Spawn State", "Default");
player = GameObject.Find ("PlayerController");
cam = GameObject.Find ("Camera");
lockCam = GameObject.Find ("Lock");
spawn = GameObject.Find ("Spawn_" + spawnState);
sceneMan = GameObject.Find ("Scene_Manager");
playScript = player.GetComponent<PlayerMovement> ();
camScript = cam.GetComponent<CameraMouseLook> ();
lockScript = lockCam.GetComponent<Lock> ();
sceneScript = sceneMan.GetComponent<Scene_Manager> ();
if (spawnState != "Default" && spawn != null)
{
camRotationRoutine = camRotation (delay);
StartCoroutine (camRotationRoutine);
spawnState = "From" + sceneScript.sceneName;
}
GameObject fader = GameObject.Find ("Screen Fader");
Fade_Screen_Scene scriptFader = fader.GetComponent<Fade_Screen_Scene> ();
scriptFader.CallStartScene ();
SaveState ();
}
public void SaveState ()
{
PlayerPrefs.SetString ("Spawn State", spawnState);
}
public void LoadDataState(Vector3 pos, Quaternion rot)
{
playPos = pos;
playRot = rot;
camRotationRoutine2 = camRotation2 (delay);
StartCoroutine (camRotationRoutine2);
}
IEnumerator camRotation (float delay)
{
camScript.blockCamera = true;
playScript.playerBlocked = true;
yield return new WaitForSeconds (delay);
player.transform.position = spawn.transform.position;
cam.transform.localRotation = spawn.transform.localRotation;
camScript.DefaultRotation ();
yield return new WaitForSeconds (delay);
camScript.blockCamera = false;
playScript.playerBlocked = false;
StopCoroutine (camRotationRoutine);
}
IEnumerator camRotation2 (float delay)
{
camScript.blockCamera = true;
playScript.playerBlocked = true;
yield return new WaitForSeconds (delay);
GameObject newSpawn = (GameObject) Instantiate (dynamicSpawn, playPos, playRot);
newSpawn.name = dynamicSpawn.name;
player.transform.position = newSpawn.transform.position;
cam.transform.localRotation = newSpawn.transform.localRotation;
camScript.DefaultRotation ();
lockScript.DefaultPosition ();
yield return new WaitForSeconds (delay);
camScript.blockCamera = false;
playScript.playerBlocked = false;
StopCoroutine (camRotationRoutine2);
}
}
The first one is the SaveSystem script that allow you to save and load game, the second one is the script that contains references of the temporary spawn.
The problem is in the second script: when player position and camera rotation have to get values of dynamicSpawn position and rotation (the temporary spawn), an error appears:
MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Start_Scene+c__Iterator9.MoveNext () (at Assets/Script/Start_Scene.cs:107)
I think that during the loading Scene, player and camera are not already loaded.
I don’t know how to fix, please help.