Change player position based on loaded scene
Hello, Im trying to make a script to change a player starting position based on a prevously loaded scene, for example.
If I go from the scene 7 to scene 2, I need the player to spawn in the p_PosiblePositions[7] in the sector 2. and if I'm going from the scene 4 to scene 2, the p_PosiblePosition to use should be [4].
I tried using this script I made but, then I realized that the OnLevelWasLoaded only tells the current scene that was loaded, not the previous one.
public GameObject[] p_PosiblePositions;
GameObject player;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
}
void OnLevelWasLoaded(int level)
{
switch (level)
{
case 0:
player.transform.position = p_PosiblePositions[0].transform.position;
break;
case 1:
player.transform.position = p_PosiblePositions[1].transform.position;
break;
case 2:
player.transform.position = p_PosiblePositions[2].transform.position;
break;
case 3:
player.transform.position = p_PosiblePositions[3].transform.position;
break;
case 4:
player.transform.position = p_PosiblePositions[4].transform.position;
break;
case 5:
player.transform.position = p_PosiblePositions[5].transform.position;
break;
case 6:
player.transform.position = p_PosiblePositions[6].transform.position;
break;
case 7:
player.transform.position = p_PosiblePositions[7].transform.position;
break;
}
}
*Each scene has his own individual p_PosiblePositions. Thanks, in advance! If I didn't explained well, please leave a comment!
Answer by hexagonius · Sep 19, 2015 at 08:52 PM
Two choices here, depending on relevance only during current game or even between game runs:
game only: create a static int variable in your choice class and set it to the current level index after evaluation before. statics are class variables and will seize to exist only when the game exits.
several games: same thing but instead of a static variable use PlayerPrefs (Unity docs)
Store the previous level.
ex 1: (using DontDestroyOnLoad())
int prevLevel = 0;
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
void OnLevel..(int level)
{
player.transform.position = p_PosiblePositions[prevLevel].transform.position;
prevLevel = level;
}
ex 2 : (using a static variable)
static int prevLevel = 0;
void OnLevel..(int level)
{
player.transform.position = p_PosiblePositions[prevLevel].transform.position;
prevLevel = level;
}
Edit : you ninja'd me by 3 seconds lol so I just converted my post to a comment.
I guess im going to try this :) but, I guess I'll need to have a lot of objects with this script attached and set the "prevLevel" correctly?
But setting the "prevLevel = level" will set the prevLevel int to the OnLevelWasLoaded, thus, the prevLevel will always be the same as the OnLevelWasLoaded int. So I guess this is the same as the script?
Please correct me if I'm wrong. or how the script that Lo0n wrote, works exactly? thanks, in advance :D
You're right, but I thought your code is the only place where that's relevant, so it doesn't matter if it's already the current level.
set it where you call LoadLevel to the loadedLevel then.