I have a script in Unity which lets me load scenes randomly once the user presses on the “Next Button” in scene 0, which in turn loads a random scene (be it scene 1 or scene 2). Now, each scene has its own next button which (theoretically) sends a message to the SceneManager script that is attached to the mainCamera object in scene 0 to loads the next random scene.
But the reality is that, while it is loading random level from scene 0 the first time, when I get to either scene 1 or 2 and hit the next button it doesn’t load, it just stops after I hit the button.
Now, here is the script I have attached to the mainacmera object in scene 0:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[ExecuteInEditMode]
public class myDefaultScene: MonoBehaviour
{
public static bool userClickedNextButton;
protected const int MAX = 2;
private List<int> scenes = new List<int>();
void Start()
{
scenes = new List<int>(Enumerable.Range(1,MAX)); // This creates a list with values from 1 to 2
cs_completionWindow = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<TestCompletionWindow>();
Debug.Log(scenes.Count);
}
void Update()
{
if (userClickedNextButton)
{
if (scenes.Count == 0)
{
Application.Quit();
}
// Get a random index from the list of remaining level
int randomIndex = Random.Range(0, scenes.Count);
int level = scenes[randomIndex];
scenes.RemoveAt(randomIndex); // Removes the level from the list
Application.LoadLevel(level);
userClickedNextButton = false;
}
}
}
Here is the command I have in my other scenes to send the message to the scene0:
public void myMethod()
{
switch(...)
//...
//CODE goes here
//...
case Windows.nextReferentTask:
myDefaultScene.userClickedNextButton = true;
break;
}