So I’m making this 2D mobile game where the player goes through around 18 levels. But I want to make it where when the player goes through the door to go to next level, it teleports player to random scene. As well as when the player finishes a level, the game remembers and ignores that level if Random.Range
picks it again.
I found this script online and it worked for a bit, but if I die in the level, the script will re-visit a level I already finished.
Here’s the script:
// paste code here
Screenshot of a text editor|572x500
My teacher suggested using for loops and appending? But I couldn’t find a video or anything, I’m very new to this, if any of you can help I would greatly appreciate it!
Here’s a simple way of how i would do it , i hope the comments explain the logic behind it
public class GoToRandomLevel
{
private List<string> scenesList;
private int currentLevelIndex;
public void GoToRandom()
{
// take the current scene at switch it's place with the first spot in the list
string tmp = scenesList[0];
scenesList[0] = scenesList[currentLevelIndex];
scenesList[currentLevelIndex] = tmp;
// then pick a random index between 1 and the end of the list
// notice how we start with 1 and not 0
// that way there's no chance the "current" scene is picked since we put it at index 0 beforehand
int randomIndex = Random.Range(1 , scenesList.Count);
currentLevelIndex = randomIndex;
SceneManager.LoadScene(scenesList[currentLevelIndex]);
}
}