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.