I want my players position to depend on wether the scene is just starting for the first time or if it was called from a different scene, I can’t seem to manage doing this. What I’m looking for is maybe a condition or script that checks if the whole game (which starts in “Scene1”) is just starting for the first time or if its being called with Application.LoadLevel later in the game.
You can create a manager script that will survive the loading / cleanup of a level.
In that script you can store all the informations you will need to determine what scene was the last one.
You can call DontDestroyOnLoad(gameObject); from your manager script and it will loading / cleanup of a level.
You should make sure to only have one instance of the manager script loaded( in case you load the scene that contains the manager object again).
If you don’t want to remember if you’ve been to a particular level before between game plays, you could keep a static bool array large enough to hold all your levels.
const int MaxLevels = 50;
static bool[] hasVisited = new bool[MaxLevels];
void OnLevelWasLoaded(int level)
{
bool visited = hasVisited[level];
hasVisited[level] = true;
if (visited)
{
// Do something
}
}
If you want to remember you’ve been to a particular level between game plays, use PlayerPrefs or write your own savegame file handling.
void OnLevelWasLoaded(int level)
{
string lvlname = Application.loadedLevelName;
bool visited = PlayerPrefs.GetInt("hasVisitedLevel_" + lvlname, 0) != 0;
PlayerPrefs.SetInt("hasVisitedLevel_" + lvlname, 1);
if (visited)
{
// Do something
}
}
Note that it can get annoying to develop with PlayerPrefs setting the level as been visited so you may want to switch to either mode depending if you’re in the editor or not.