Hey guys. I have a problem in loading a data from json. My save button is working so far, but my load button its not properly functioning. What I did is, my save button can be see on in-game while the load button is on the main menu. So in the in-game, I can save the scene name and current position of my character, but when I click the load button on the menu, the scene will open what’s on the json. For example the sceneName is “StartQuest” then it can load it. My problem is how can I load the position and rotation of my character base on the json? Here’s what I did:
public class GameSaving {
public string sceneName;
public float[] position;
public float[] rotation;
}
**SaveLoadManager.cs**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SaveLoadManager : MonoBehaviour {
public static SaveLoadManager saveLoadManager;
private string jsonSavePath;
public GameSaving gameSaving;
[SerializeField] private Player player;
[SerializeField] private Button saveButton;
[SerializeField] private Button loadButton;
private string savedSceneName;
void Awake() {
if (saveLoadManager == null) {
saveLoadManager = this;
} else if (saveLoadManager != this) {
Destroy (gameObject);
}
DontDestroyOnLoad (gameObject);
}
void OnEnable() {
gameSaving = new GameSaving ();
saveButton.onClick.AddListener(delegate { OnSaveButtonClick(); });
loadButton.onClick.AddListener (delegate { OnLoadButtonClick(); });
//LoadData ();
}
void Start() {
jsonSavePath = Application.persistentDataPath + "/saveload.json";
}
public void OnSaveButtonClick() {
SaveData();
Debug.Log ("Saved");
}
public void OnLoadButtonClick() {
LoadData ();
Debug.Log ("Loaded");
}
private void SaveData() {
//References
Scene scene = SceneManager.GetActiveScene ();
//Scene Name
gameSaving.sceneName = scene.name;
//Position
gameSaving.position = new float[3];
gameSaving.position [0] = player.transform.position.x;
gameSaving.position [1] = player.transform.position.y;
gameSaving.position [2] = player.transform.position.z;
//Rotation
gameSaving.rotation = new float[3];
gameSaving.rotation [0] = player.transform.rotation.x;
gameSaving.rotation [1] = player.transform.rotation.y;
gameSaving.rotation [2] = player.transform.rotation.z;
string jsonData = JsonUtility.ToJson (gameSaving, true);
File.WriteAllText (jsonSavePath, jsonData);
}
public void LoadData() {
gameSaving = JsonUtility.FromJson<GameSaving>(File.ReadAllText(Application.persistentDataPath + "/saveload.json"));
savedSceneName = gameSaving.sceneName;
Vector3 position;
position.x = gameSaving.position [0];
position.y = gameSaving.position [1];
position.z = gameSaving.position [2];
transform.position = new Vector3(position.x, position.y, position.z);
Quaternion rotation;
rotation.x = gameSaving.rotation [0];
rotation.y = gameSaving.rotation [1];
rotation.z = gameSaving.rotation [2];
transform.rotation = Quaternion.Euler(rotation.x, rotation.y, rotation.z);
SceneManager.LoadScene (savedSceneName);
//For testing purposes
Debug.Log (transform.position);
Debug.Log (transform.rotation);
Debug.Log (savedSceneName);
}
}
So I created an empty object on StartQuest scene and Main Menu Load. This what I did and I got these error when I start the MainMenu. Object reference not set to an instance of an object
SaveLoadManager.OnEnable () (at Assets/Scripts/SaveGame/SaveLoadManager.cs:33)
In Start Quest:
In Main Menu (Load Button)