Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues loading different levels randomly in Unity-3D
    text
    copied!<p>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 the user clicks on the "Next Button" and once all levels have been loaded, we stop loading and close the application. I got the code set up but I am still not getting the result I am looking for which is:</p> <ol> <li><p>User clicks on a button.</p></li> <li><p>Load a random level</p></li> <li><p>That levels gets stored in an array list</p></li> <li><p>Once the user is done with that level he/she presses the "Load Next Level" button</p></li> <li><p>Load the next random level</p></li> <li><p>But first, we check if the random level is not the same as before.</p></li> <li><p>If it's not, then we repeat steps 2-5, else we go to step 8</p></li> <li><p>If the levels have all been visited then we quit the application</p></li> </ol> <p>The problem I am having is that my game loads the same level every time I hit play and it doesn't go to the next scene after I am done with the current one. This is what I have so far:</p> <pre><code>using UnityEngine; using System.Collections; [ExecuteInEditMode] public class SceneManager : MonoBehaviour { public static bool userClickedNextButton; //This flag is raised by the other classes that have the GUI button logic protected const int MAX = 2; private ArrayList scenesWereAlreadyLoaded = new ArrayList(); void Update() { if (userClickedNextButton) { //by default the game starts at 0 so I want to be able to //randomly call the next two scenes in my game. There will //be more levels but for now I am just testing two int sceneToLoad = Random.Range(1, 2); if (!scenesWereAlreadyLoaded.Contains(sceneToLoad)) { scenesWereAlreadyLoaded.Add(sceneToLoad); Application.LoadLevel(sceneToLoad); } userClickedNextButton = false; } if (scenesWereAlreadyLoaded.Count &gt; MAX) { Application.Quit(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload