Alright, I have a terribly difficult problem for you guys out there (either that, or I’m a simple person :P):
So I have a saving system set up for my game that, so far, saves the player’s exact position as well as a variable labeled “houseProgress” that essentially makes sure the game progress through its story:
Script that saves:
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameControl : MonoBehaviour {
public static GameControl control;
public GameObject player;
public Vector3 pos;
public float houseProgress;
public PlayerActivate playerActivateScript;
void Awake(){
player = GameObject.FindGameObjectWithTag ("Player");
playerActivateScript = player.GetComponent<PlayerActivate> ();
if (control == null) {
DontDestroyOnLoad (gameObject);
control = this;
} else if (control != this) {
Destroy (gameObject);
}
}
void Update(){
if (player == null) {
player = GameObject.FindGameObjectWithTag ("Player");
playerActivateScript = player.GetComponent<PlayerActivate> ();
}
}
public void Save()
{
//playerPosition
PlayerPrefs.SetFloat ("x", player.transform.position.x);
PlayerPrefs.SetFloat ("y", player.transform.position.y);
PlayerPrefs.SetFloat ("z", player.transform.position.z);
PlayerPrefs.SetFloat ("houseProgress", playerActivateScript.houseProgress);
}
public void Load()
{
player.transform.position = new Vector3 (PlayerPrefs.GetFloat ("x"), PlayerPrefs.GetFloat ("y"), PlayerPrefs.GetFloat ("z"));
}
}
This script does alright when it comes to saving the player’s position. The only problem is that, when the scene changes (which always happens alongside the save script), I get a TON of null reference errors in some scenes, and for good reason because the player object does not exist in some scenes I have set up, so hopefully someone can help me deal with this problem.
So that’s the first problem, the second regarding the save script is the houseProgress variable. It is a float that increases by one every time the player does something. However, it doesn’t seem to want to save what it is at. Here is the script I have set up so far that affects it:
if (check.isColliding == true) {
houseProgress += 1;
GameControl.control.Save ();
SceneManager.LoadScene ("Journal Entry 1");
}
I think I know the problem may lie with the variable is tied to the player, which isn’t in the Journal Entry scene, so it resets back to the value of 0, so I want to know how I can make this persist.
And that is it! If anyone needs ANY information, please let me know!