I have a class that handles loading levels randomly as illustrated by this diagram: ![alt text][1]
[1]: /storage/temp/19700-loadscenes.png
This is the class I created:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[ExecuteInEditMode]
public class ReferentManager : MonoBehaviour
{
FinalGUIWindow cs_finalWindow;
protected const int MAX = 2;
private List scenes;
void Start()
{
cs_finalWindow = GameObject.FindGameObjectWithTag("MainCamera").GetComponent();
scenes = new List(Enumerable.Range(1,MAX)); // This creates a list with values from 1 to 2
OnLevelWasLoaded(0);
DontDestroyOnLoad(gameObject);
}
void Update()
{
if (scenes.Count <= 0)
{
cs_finalWindow.showCompletedWindow();
}
}
void OnLevelWasLoaded(int index)
{
var defaultScene = GameObject.FindObjectOfType();
if (defaultScene != null)
{
defaultScene.OnLoadNextLevel -= OnLoadNextLevelHandler;
defaultScene.OnLoadNextLevel += OnLoadNextLevelHandler;
}
var nextScene = GameObject.FindObjectOfType();
if (nextScene != null)
{
nextScene.OnLoadNextLevel -= OnLoadNextLevelHandler;
nextScene.OnLoadNextLevel += OnLoadNextLevelHandler;
}
var lastScene = GameObject.FindObjectOfType();
if (lastScene != null)
{
lastScene.OnLoadNextLevel -= OnLoadNextLevelHandler;
lastScene.OnLoadNextLevel += OnLoadNextLevelHandler;
}
}
private void OnLoadNextLevelHandler()
{
int randomIndex = Random.Range(0, scenes.Count);
int level = scenes[randomIndex];
scenes.RemoveAt(randomIndex); // Removes the level from the list
Application.LoadLevel(level);
}
}
The class doses what it should be doing but once it gets to a certain point in the game it gives me the following issues:
->When I load the first random scene everything runs fine.
->When I load the second random scene it gives me this error:
Line 25: NullReferenceException: Object reference not set to an instance of an object
ReferentManager.Update ()
->The strange thing is that I can run through this scene fine in spite of all the debug log errors is spamming.
->Then after I am done with that scene the debug window spams the next debug errors:
Line 52: ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[System.Int32].get_Item (Int32 index)
ReferentManager.OnLoadNextLevelHandler () (at Assets/Scripts/General Scripts/ReferentManager.cs:52)
Cube_SceneManager.displayScene () (at Assets/Scripts/GUI Scripts/Cube Scene/Cube_SceneManager.cs:58)
Cube_SceneManager.OnGUI () (at Assets/Scripts/GUI Scripts/Cube Scene/Cube_SceneManager.cs:35)
Can anyone help me understand why I am getting these errors? Thank you in advance and happy holidays!
↧