Load levels randomly

Greetings! I am working on this game for school and I want to know how to get this feature started: I need to create a class (or classes, if need be) that loads levels randomly every time and once all levels have been loaded, we stop loading and close the application.

Here is my code snippet:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class SceneManager : MonoBehaviour {
    public enum LoadMyScene
    {
        scene1, scene2, scene3 
    }
    [HideInInspector]
    public static int nextscene = Random.Range(0, 2); //need this when the user clicks on the "next" button  

    public LoadMyScene currentScene = (LoadMyScene)nextscene;

    private int counter; //increases every time a scene is loaded
    protected const int MAX = 3;

    void Start()
    {
        counter = 0;
    }
    void Update()
    {
        if (counter > MAX) { Application.Quit(); }
		
		switch (currentScene)
        {
            case LoadMyScene.scene1:
                Application.LoadLevel(1);
                break;
            case LoadMyScene.scene2:
                Application.LoadLevel(2);
                break;
            case LoadMyScene.scene3:
                Application.LoadLevel(3);
                break;
        }
    }
}

As you can see there are several key elements missing and I want to know what am I missing to get this class to a working state.

Hi there
in your example it looks you loading level in update method without any additional check / stops or state changes

where is your track of current scene? you need to implement some kind of routine to change your state and also load level only once, but not on each update

also you didn’t specify is it allowed to load the same scene repetitively for example if scene 1 was loaded then scene 2 was loaded and Random.Range(0, 2) will give you again 1 which was already loaded what would be in that case?

it should looks like that:

public class SceneManager : MonoBehaviour {

	private ArrayList scenesWereAlreadyLoaded = new ArrayList(); 
 
    void Start()
    {
        int currentSceneToLoad; =  Random.Range(0, 2);
		scenesWereAlreadyLoaded.add(currentSceneToLoad);
		Application.LoadLevel(currentSceneToLoad);
    }
    void Update()
    {
        if(userClickedNextButton)
		{
			while(true)
			{
				int sceneToLoad = Random.Range(0, 2);
				if(!scenesWereAlreadyLoaded.Contains(sceneToLoad))
				{
					scenesWereAlreadyLoaded.add(sceneToLoad);
					Application.LoadLevel(sceneToLoad);
					break;
				}
			}
		}
		
		if(scenesWereAlreadyLoaded.Length > 2)
			Application.Quit();
		
    }
	
}

but even this is very messy and you should optimise it for your current task, this is just quick example to demonstrate you the logic

var scenesWereAlreadyLoaded = new ArrayList();
var currentSceneToLoad : int;

currentSceneToLoad = Random.Range(1, Application.levelCount);
scenesWereAlreadyLoaded.Add(currentSceneToLoad);
Application.LoadLevel(currentSceneToLoad);

if(userClickedNextButton){
while(true){
currentSceneToLoad = Random.Range(1, Application.levelCount);
		 if(!scenesWereAlreadyLoaded.Contains(currentSceneToLoad)){					 				  scenesWereAlreadyLoaded.Add(currentSceneToLoad);
Application.LoadLevel(currentSceneToLoad);
break;
}