Randomize Which Level Comes Next

I’m very new to Unity. I’ve been reading tutorials and watching video tutorials and learning as much as I can. I have a “basic” game idea that I’m trying to create in hopes that the experience of it will help me learn how to do everything I want to do for future games. Basically, the problem I’m currently on is I can’t find a tutorial on randomizing levels. When I look for tutorials, all I’m able to find is tutorials on randomizing level paths and traps and monsters and all of that but what I need is say I have a list of 15 levels that are all made fully, I want which level comes up next after beating the current level, to be random and then removed from the list so it doesn’t appear again. So, can someone please explain this to me or point me to a great tutorial on this? I can’t figure it out and it would be a HUGE help. Thank you very much for any help I receive. =)

I think this will help you:

// create a list object
System.Collections.Generic.List<string> levelsList = new System.Collections.Generic.List<string>();

void PopulateLevelsList()
{
    // populating the list with all of levels
    levelsList.Add("level1");
    levelsList.Add("level2");
    levelsList.Add("level3");
    // and so on
}

void GoToNextLevel()
{
    if (levelsList.Count == 0)
    {
        // All levels finished
    }
    else
    {
        // create a random number between 0 and remaining levels count
        int randomIndex = UnityEngine.Random.Range(0, levelsList.Count);

        // load the random selected level
        Application.LoadLevel(levelsList[randomIndex]);

        // removing selected level from the list
        levelsList.RemoveAt(randomIndex);
    }
}

Call PopulateLevelsList() in the Start() and then call GoToNextLevel() any time you wanted to load next level.

tell me if anything looks confusing to you.

BTW this only works in a single run. It doesn’t save the levels that the player passed. If you need something permanent, just tell me.

Thank you very much! It makes enough sense that I can look up any parts I need to understand more. However, you said that it only works in a single run. May I ask what you mean? I won’t need it to save levels the player has passed as they will get a certain amount of points for levels they pass and possibly lose points for levels they don’t pass. I just want to make sure that a level is not appearing again after it opens for the first time.

If you’re curious, I’m creating a trivia sort of game. I’m still working out how I want it all to go but it’ll be a single player (would love multiplayer but I don’t even know where to begin for multiplayer yet) and the goal is to raise your score up as high as possible. Each question will range in topic and difficulty and points gained for getting the correct answer will vary depending on that level of difficulty. I’m not 100% I’m sticking with this idea but as of right now, that is what I’m going for unless I can think of better ways of doing this.

Sorry for late response.

I meant that it doesn’t save the questions you already answered. So you can’t continue your game the next time you played the game. But now that you explained your game, I think it is OK for you.

Although, if you have so much questions in your game (like a thousand or more) I think a different approach must be better for your game. Because it is very memory consuming that have all of them in a list and remove from that list one by one.

I think it is better to just keep the indexes of the questions you already asked, and next time you wanted to ask a question, check if the new question index isn’t in the list.