1

I have a game where there are multiple rounds. Each round has a different map and at the end of the round a random scene is loaded from a list of scenes. I have a list of scenes like:

Arena 1
Arena 2
Arena 3
Arena 4
Arena 5

And I randomly load them when the round is over using this line of code:

Application.LoadLevel("Arena " + Random.Range(1, LEVEL_COUNT));

where LEVEL_COUNT is the max number of scenes I have.

This works fine, except sometimes when the round is over, the same scene that has just been played reloads. Say if you play Arena 3 and the round is over, Arena 3 may reload again.

So, is it possible to load a random scene like I am doing, but not load a scene that is the same scene we have just played? So load anything BUT Arena 3 in this case.

Thanks for the help!

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
Tom
  • 2,372
  • 4
  • 25
  • 45

1 Answers1

4

I believe you are saying that you can repeat scenes, but not consecutively, is that correct? So you could play Arena 3 multiple times, as long as there is another scene in between each time.

If that's the case, then I would do it like so:

//Store the current scene in an integer. Set to zero by default.
int previousScene = 0;

The first time you get your random scene, there are LEVEL_COUNT number of scenes to choose from. But thereafter, since you can't repeat the scene, it's actually LEVEL_COUNT - 1.

//Randomly load the scene
int nextScene;
if(previousScene == 0)
    nextScene = Random.Range(1, LEVEL_COUNT);
else
    nextScene = Random.Range(1, LEVEL_COUNT - 1);

Every time the RNG returns a level that is the same or higher as the previous level, just add 1 to it. Then store the previous scene, and repeat.

if(nextScene >= previousScene){
    nextScene = nextScene + 1;
}
Application.LoadLevel("Arena " + nextScene);
previousScene = nextScene;
Karmacon
  • 3,128
  • 1
  • 18
  • 20