Checkpoints between scenes (NullReferenceException?)
Hi! I am creating my first dungeon crawler game in Unity and am in need of assistance. I want to use a checkpoint system so that after collecting an object in a scene, my game changes the player's starting position upon returning to a different scene. Ex. If the collectible from level 2 is found, when the player returns to level 1 they will start in a different position.
I tried to do this by creating a public variable in a public global control class to hold the position between scenes, which can hold the position of the local checkpoint and return it when the player comes back to the checkpoint level. This however, causes the following error when called:
NullReferenceException: Object reference not set to an instance of an object.
Below is my Checkpoint class in which I attempt to
public static Vector3 checkpointPos;
public static GameObject player;
//Lists all gatepoints in the scene
public static GameObject[] Checkpoints;
//public instance of a class
public static Checkpoint instance;
void Awake()
{
//Finds all checkpoints in the current scene
Checkpoints = GameObject.FindGameObjectsWithTag("Checkpoint");
//Assigns player object to the player itself
player = GameObject.FindGameObjectWithTag("Player");
FindCheckpoint();
}
/*Determine which checkpoint to use based on # of collectibles
**Ex. If player has not collected 2 collectibles when in gates scene,
**it will return to the door to Level 2 until otherwise.*/
public void FindCheckpoint()
{
switch (CollectibleControls.collectibles)
{
case 1:
print("Called case 1");
checkpointPos = Checkpoints[0].transform.position;
break;
case 2:
print("Called case 2");
checkpointPos = Checkpoints[1].transform.position;
break;
case 3:
print("Called case 3");
checkpointPos = Checkpoints[2].transform.position;
break;
case 4:
print("Called case 4");
checkpointPos = Checkpoints[3].transform.position;
break;
}
SavePositions();
}
//determine if the global variable is holding a checkpoint; if not,
//it will find one based on the number of collectibles
public void DeterminePosition()
{
if (GlobalControl.checkpointPos != null)
{
player.transform.position = GlobalControl.checkpointPos;
}
else
{
FindCheckpoint();
}
}
void SavePositions()
{
//Save positions to Global control to keep between scenes
GlobalControl.checkpointPos = checkpointPos;
}
And here is my variable in global control: public class GlobalControl : MonoBehaviour { public static Vector3 checkpointPos; } It is called only by my game manager when the player returns to the scene with the checkpoint system, which is referred to as gateLevel:
public void LevelControls(Collider other)
{
SceneManager.LoadScene(doorLevel);
if (doorLevel == gateLevel)
Checkpoint.instance.DeterminePosition();
}
Thank you so much for reading this! I am new to the Unity community and would greatly appreciate the help.
Your answer
Follow this Question
Related Questions
How to move/lock camera upon checkpoint? 1 Answer
NullReferenceException, Script Error 1 Answer
GetComponent returns null 1 Answer
NullReferenceException 0 Answers
Reference lost in NetworkEntity 0 Answers