- Home /
Removing scenes with set conditions
I have this task where I have to set up 3 cameras for each of my scenes (a total of 10 scenes). Next, I must somehow make them load randomly every time the user starts the game. So, ideally, the user starts from a different viewport each time. In addition I have to keep them in a list.
I went ahead and did that:
created the camera list
Initialized the 3 cameras that I am going to use
Stored those 3 cameras into the camera list
(I hope you are still following me so far)
In addition to the camera task I have to set up a class where I have to store each of my scenes into a list and then load the scenes randomly. Thus, every time the user starts the program a new scene is loaded.
I went ahead and did that:
In my global variable region: public static List scenes;
In my Start() method: scenes = new List(Enumerable.Range(1, MAX)); // This creates a list with values from 1 to MAX
I have MAX set up to 9 since I have 10 scenes created.
Next, I handle each scene being loaded in this method which I am treating as an event. Forgive my hacky approach:
private void OnLoadNextLevelHandler()
{
if (scenes.Count > 0)
{
int randomIndex = Random.Range(0, scenes.Count);
int level = scenes[randomIndex];
Application.LoadLevel(level);
scenes.RemoveAt(randomIndex); //once I am done with the scene, remove it
}
}
It works like a charm! Every scene is being randomly picked with a random camera each time and it looks great!
However, as we enter into the new phase of our student project some new features have been requested and it is here where I am having issues:
I want to be able to load each scene randomly and start from a different camera angle each time (like before) but this time I have to be able to go back to the same scene and load one of the randomly selected viewport that remain in the camera list.
I have to be able to keep going back to that scene while there are viewports remaining. When a scene has run out of cameras then that scene is removed from the list. Once all scenes have been removed from the list then a window pops up congratulating the user for completing the task and there is a button that, when clicked, closes the application.
The only thing I have changed is add a condition when the program loads a level. Apologies for taking such a hacky approach:
//Making sure that all "viewports" list from each scenes are not empty. Basically I want to be able to load the same scene more than once if there are any cameras left in that scene...
if(Scene1_CameraHandler.viewPorts.Count > 0 || Scene2_CameraHandler.viewPorts.Count > 0 || Scene3_CameraHandler.viewPorts.Count > 0 ||
Scene4_CameraHandler.viewPorts.Count > 0 || Scene5_CameraHandler.viewPorts.Count > 0 || Scene6_CameraHandler.viewPorts.Count > 0 ||
Scene7_CameraHandler.viewPorts.Count > 0 || Scene8_CameraHandler.viewPorts.Count > 0 ||
Scene9_CameraHandler.viewPorts.Count || Scene10_CameraHandler.viewPorts.Count)
{
Application.LoadLevel(level);
}
else
scenes.RemoveAt(randomIndex); // ... if there are no more cameras then remove the level from the list
}
To remove the current viewport I put this code at the end of each scene when the user clicks the "Done" button to load the next random scene:
Scene1_CameraHandler.viewPorts.Remove(Scene1_CameraHandler.viewPorts[Scene1_CameraHandler.randomCameraSelectorIndex]);
This loads the level and the viewports just fine (as it did before) but now when it tries to go to a previous scene the program just stops and gives me an error stating: ArgumentOutOfRangeException: Argument is out of range.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132542im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
IS it possible to load the whole game (and all scenes) at startup to avoid scene change delay? 1 Answer
Distribute terrain in zones 3 Answers
Streamed scenes in standalone build 0 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer