reload random scene

I have a code for random scenes, the scenes that I have are 10 randomized scenes, but when I reload the scene there is only one scene.

public static int pointAdd;
    //menyimpan angka yang akan di random
    private List<int> myNiceNumbers = new List<int>();
    //nama scene yang akan tampil
    private List<string> sceneNames = new List<string>() { "GameDD10", "GameDD20", "GameDD30", "GameDD40"};

    private void Awake()
    {
        //objeknya berjalan terus walaupun ganti scene
        DontDestroyOnLoad(this.gameObject);
    }

    private void Start()
    {
        for (int i = 0; i < 5; i++)
        {
            myNiceNumbers.Add(i);
        }
    }

    public void NextScene()
    {
        if (myNiceNumbers.Count != 0)
        {
            int randomIndex = Random.Range(0, myNiceNumbers.Count);
            int randomNumber = myNiceNumbers[randomIndex];

            myNiceNumbers.RemoveAt(randomIndex);

            SceneManager.LoadScene(sceneNames[randomNumber]);
        }
        else
        {
            SceneManager.LoadScene("GameDDComplete");
        }
    }

You have to also add your scenes to your build settings. Open up build settings and all the scenes that can be loaded will be at the top with a check mark. If they are not, open the scene, then add it.


 int index = Random.Range(0,10);
 SceneManager.LoadLevel(index);
 //SceneManager.LoadLevel(Random.Range(0, Application.levelCount));    // or this way

Try this instead


Also, you only have 4 sceneName strings, but you are creating 5 nice numbers. Some out of range action can occur.


private List<string> sceneNames = new List<string>() { "GameDD10", "GameDD20", "GameDD30", "GameDD40"};     // Only 4 strings
 
     
     private void Start()
     {
         for (int i = 0; i < 5; i++)      // But you create 5 indexes
         {
             myNiceNumbers.Add(i);
         }
     }

I have 10 questions in different scenes. and I tried to make the scene display randomly. this has been successful using this code:

 public int pointsAddedForCorrectAnswer;
    //menyimpan angka yang akan di random
    private List<int> myNiceNumbers = new List<int>();
    //nama scene yang akan tampil
    private List<string> sceneNames = new List<string>()
    { "GameTiga1", "GameTiga2", "GameTiga3", "GameTiga4", "GameTiga5", "GameTiga6", "GameTiga7", "GameTiga8", "GameTiga9", "GameTiga10"};

    private void Awake()
    {
        //objeknya berjalan terus walaupun ganti scene
        DontDestroyOnLoad(this.gameObject);
    }

    private void Start()
    {
        for (int i = 0; i < 10; i++)
        {
            myNiceNumbers.Add(i);
        }
    }

    public void NextScene()
    {
        if (myNiceNumbers.Count != 0)
        {
            int randomIndex = Random.Range(0, myNiceNumbers.Count);
            int randomNumber = myNiceNumbers[randomIndex];

            myNiceNumbers.RemoveAt(randomIndex);

            SceneManager.LoadScene(sceneNames[randomNumber]);
        }
        else
        {
            SceneManager.LoadScene("RoundEndGameTiga");
        }
    }

and this is the code for each scene :

 public void NextQuestion()
    {
        GameObject.Find("RandomSceneGame").GetComponent<RandomScene>().NextScene();
        
    }

 public void Reload()
    {
        SceneManager.LoadScene("MenuDragDrop");
    }

but when I add the reload function, to repeat the game, it can’t load all 10 scenes. but only 1 scene.