Need character to spawn at a position they already have passed

I have been working on this problem for a couple of weeks now really trying to figure this out on my own. Unfortunately I have tried everything I know and simply can’t get this to work.

I have a character that the user controls. Along the way the character will pass position points so that if the character dies beyond a point it is supposed to spawn back at the most recent position point it passed. This way the user doesn’t have to start all the way back at the beginning.

Here is the code I use to preserve various data from scene to scene, i.e player lives, score etc .

private void Awake()
    {
        int numberOfGameSessions = FindObjectsOfType<GameSession>().Length;

        bob = FindObjectOfType<Bob>();

        if (numberOfGameSessions > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
           
        }
    }

This works great for all of the other variables in the script that I need to preserve however for the one I use to let me know which position point the user just passed, it keeps resetting back to zero which is the starting position point.

I have also tried using:

PlayerPrefs.SetInt("SpawnPosition",markerPosition);

to save the position point the character just passed and then this one to load in that position point when the character respawns:

[SerializeField] Transform[] spawnPoint;

transform.position = spawnPoint[PlayerPrefs.GetInt("SpawnPosition")].position;

The serialize field is in the declare area at the top of the script. I just put it here to show you were that variable comes from.

I then assign the position marker game objects to the character to complete that variable.

Once the character dies though the value of

PlayerPrefs.GetInt("SpawnPosition")

And when I use “SpawnPosition” as a variable in the game session script always comes back at zero.

I am absolutely lost and confused at this point.

Any ideas would be greatly appreciated.

Feel free to ask me any questions if this is unclear.

I’m confused about your PlayerPrefs. For one that should only be used to save data for reloading the game. For two I think you are using PlayerPref wrong, PlayerPrefs(string key, int defaultValue), meaning the first thing is your key so let’s say slot 0 and second is your data let’s say a Vector3 position; PlayerPrefs.SetInt(“0”, (int)player.transform.position.x). I would use string for saving then parse the string on game load; PlayerPrefs.SetString(key,position).

I would create a spawn point however if not needed to persist after game is shut down.

 Vector3 spawnPoint;

void SaveNewPosition()
{
   spawnPoint = player.transform.position;
}

void Respawn()
{
   player.transform.position = spawnPoint;
}

Where would you put the code that you just wrote though? That is where I am having trouble. I have done similar code as you have written but the spawnPoint resets back to the original starting point. I placed it in my GameSession script which contains the “Awake()” code mentioned above.

Put it in whatever script your using.

When you want to save a new position then call SaveNewPosition and pass the position if you don’t have reference to the player. So let’s say your in your player script then you would just pass that position; SaveNewPosition(transform.position).

  • void SaveNewPosition(Vector3 playerPosition)
  • {
  • spawnPoint = playerPosition;
  • }

Then when you want to respawn just call Respawn as spawnPoint now holds the position you saved.

  • void Respawn()
  • {
  • player.transform.position = spawnPoint;
  • }

Your Player is a GameObject and that has a Transform which has a position stored as a Vector3. You want to store that position into another Vector3 which I called spawnPoint. To put your player back into that position you set your player’s position to that Vector3; GameObject.transform.position = Vector3.