- Home /
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.
Consider an approach where you have an array of integers, and each entry in the array is a valid level number (something that could be passed as an argument to Application.LoadLevel()). If you then randomly shuffle that array, you now have a random ordering of the levels. This Wikipedia page might give you some ideas on how to randomly shuffle an array.
@Guts: thank you for the reply, the explanation is pretty good to understand, however, could you show me an example in code of how to start this? I'm a little new to this. Thank you!
You want to repeatedly load a random level (each level loading immediately after the previous one has finished loading such that almost no time is spent in each level) until every level has been loaded. Am I understanding this correctly?
@TinkerinThinker: no, each level has a button that,after the user clicks on it, it loads a random scene. But I do not want the same level being loaded over and over so I need a tracker that will keep count of which level is being loaded and stores it so that the next time the user clicks on the load scene the same scene is not loaded again. Once all scenes have been loaded then the application will close.
Answer by Briksins · Dec 17, 2013 at 04:27 PM
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
@Briksins Thank you for your reply. What you wrote made a lot of sense but for some reason it doesn't translate well into my design. Could you show me how to fuse what you wrote into $$anonymous$$e to see where everything should go? Thank you.
@Briksins: hi, this is what I managed to get but it still doesn't want to work.
using UnityEngine;
using System.Collections;
[ExecuteInEdit$$anonymous$$ode]
public class Scene$$anonymous$$anager: $$anonymous$$onoBehaviour
{
public static bool userClickedNextButton;
protected const int $$anonymous$$AX = 2;
private ArrayList scenesWereAlreadyLoaded = new ArrayList();
void Update()
{
if (userClickedNextButton)
{
while (true)
{
int sceneToLoad = Random.Range(1, 2);
if (!scenesWereAlreadyLoaded.Contains(sceneToLoad))
{
scenesWereAlreadyLoaded.Add(sceneToLoad);
Application.LoadLevel(sceneToLoad);
break;
}
}
}
if (scenesWereAlreadyLoaded.Count > $$anonymous$$AX) { Application.Quit(); }
}
}
Answer by Hoskins355 · Apr 24, 2014 at 02:47 PM
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;
}
Your answer
Follow this Question
Related Questions
Follow-up from previous question: "Load levels randomly" 0 Answers
Multiple Cars not working 1 Answer
Random message generator out of control 1 Answer
Making a camera list 1 Answer
Unity random vs C# random 2 Answers