How to Make a Checkpoint for a 3D game!

Hi

I’m working on a 3D endless runner game and when my player falls off an edge (y < 2), my scene reloads to let the player play again. It works well but I’m trying to add a checkpoint that gets activated after a distance (Z-axis = 2400) has been run past. I’m using the script below to achieve what I want. (I’ve already grabbed a reference to the “RB” on this script.)

if (SceneManager.GetActiveScene().name == “Level 7”)
{
if (RB.position.z > 2400)
{
if (RB.position.y < 2)
{
FindObjectOfType().EndGame2();
}
}

Then after taking me to the method “EndGame2” on a new script named “GameManager”, it continues:

public GameObject Player;

private void Awake()
{
Player = GameObject.Find(“Player”);
DontDestroyOnLoad(gameObject);
}

public void EndGame2()
{
Invoke(“Restart2”, 4f);
}

public void Restart2 ()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    Player.transform.position = new Vector 3 (0, 1, 2400);
}

Now, the main problem is that once the level reloads, the player starts moving form still the point zero on Z-axis (not 2400) and it seems the “DontDestroyOnLoad” doesn’t work well. I know I may have had many mistakes. I would appreciate it if someone please helps me with a clear explanation. Thanks in advance.

Hi Siavash198,

The Player variable you’re modifying in that method is the one from the current scene. If you replace the player when the scene loads, that would be why it seems to not move. You can debug this by using the GetInstanceID(); for example (assuming that Player is a gameObject):

public void Restart()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    Debug.Log("What's my player's ID: " + Player.GetInstanceID());
}

void Start()
{
    Debug.Log("What's my player's ID at start: " + Player.GetInstanceID());
}

You will then see if the object you’re modifying the position on is the same instance as the one you end up with after the scene load.

I hope this helps.