Hello again,
I am having a couple problems with my save script again. I was just wondering why it is giving me the errors ‘An Object Reference is required to access a non-static member ‘UnityEngine.Component.transform’’. Also, because of this, it will not let me access any of the scripts in the inspector. Ignore the right part, it is my other screen on the screenshot. Here is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveScript {
public static GameObject player;
public static GameObject notFound;
public void Save() {
BinaryFormatter binary = new BinaryFormatter ();
FileStream fStream = File.Create (Application.persistentDataPath + "/saves/slot1.byes");
SaveManager saver = new SaveManager ();
saver.campos = Camera.transform.position;
saver.pos = player.transform.position;
saver.level = Application.loadedLevelName;
binary.Serialize (fStream, saver);
fStream.Close ();
}
public void Load() {
if (File.Exists (Application.persistentDataPath + "/saves/slot1.byes")) {
BinaryFormatter binary = new BinaryFormatter ();
FileStream fStream = File.Open (Application.persistentDataPath + "/saves/slot1.byes", FileMode.Open);
SaveManager saver = (SaveManager)binary.Deserialize (fStream);
fStream.Close ();
player.transform.position = saver.pos;
Camera.transform.position = saver.campos;
Application.LoadLevel (saver.level);
} else {
notFound.SetActive(true);
System.Threading.Thread.Sleep(5000);
notFound.SetActive(false);
}
}
}
[System.Serializable]
class SaveManager {
public Vector3 pos;
public Vector3 campos;
public string level;
}
