In each level, keep an object with a script on it that keeps track of spawnpoints, with an array of transforms or something. Give it a unique tag, such as "SpawnInfo", and then have a second script which keeps track of which point to spawn at for each level, with an array of integers. The second script should use DontDestroyOnLoad, to stop it from getting deleted when you change scenes.
In your 'GameEngine' script (or whatever you use to keep track of persistent ingame information)
public int[] levelSpawns;
void Start ()
{
levelSpawns = new int[Application.levelCount];
gameObject.tag = "Engine";
DontDestroyOnLoad(gameObject);
}
Then, when you leave a level, have a few lines that go like this-
public void LeaveLevel(int nextLevel, int targetSpawnPoint)
{
GameEngine engine = GameObject.FindWithTag("Engine").GetComponent<GameEngine>();
engine.levelSpawns[nextLevel] = targetSpawnPoint;
Application.LoadLevel(nextLevel);
}
Then on your player (when you load the level), set the position correctly, and you're done!
void OnLevelWasLoaded(int newLevel)
{
GameEngine engine = GameObject.FindWithTag("Engine").GetComponent<GameEngine>();
int spawnNumber = engine.levelSpawns[newLevel];
SpawnManager manager = GameObject.FindWithTag("SpawnInfo").GetComponent<SpawnManager>();
Transform spawnPoint = manager.spawnPoints[spawnNumber];
transform.position = spawnPoint.position;
transform.rotation = spawnPoint.rotation;
}
(Posted in C# because you didn't specify, and I prefer the language. If it's that important to you, I can translate it to JS if you need it)
EDIT Heres some JS for people who want that.
ENGINE-
var levelSpawns : int[];
function Start ()
{
levelSpawns = new int[Application.levelCount];
gameObject.tag = "Engine";
DontDestroyOnLoad(gameObject);
}
Bit for exiting level-
function LeaveLevel(nextLevel : int, targetSpawnPoint : int)
{
var engine : GameEngine = GameObject.FindWithTag("Engine").GetComponent(GameEngine);
engine.levelSpawns[nextLevel] = targetSpawnPoint;
Application.LoadLevel(nextLevel);
}
Bit on level load
function OnLevelWasLoaded(newLevel : int)
{
var engine : GameEngine = GameObject.FindWithTag("Engine").GetComponent(GameEngine);
var spawnNumber : int = engine.levelSpawns[newLevel];
var manager : SpawnManager= GameObject.FindWithTag("SpawnInfo").GetComponent(SpawnManager);
var spawnPoint : Transform = manager.spawnPoints[spawnNumber];
transform.position = spawnPoint.position;
transform.rotation = spawnPoint.rotation;
}