Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mmangual_83 · Dec 24, 2013 at 01:51 PM · c#guiload

Getting Debug log errors when I load my scenes

I have a class that handles loading levels randomly as illustrated by this diagram: alt text

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<int> scenes;
 
     void Start()
     {
         cs_finalWindow = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<FinalGUIWindow>();
         scenes = new List<int>(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<FirstScene>();
         if (defaultScene != null)
         {
             defaultScene.OnLoadNextLevel -= OnLoadNextLevelHandler;
             defaultScene.OnLoadNextLevel += OnLoadNextLevelHandler;
         }
         var nextScene = GameObject.FindObjectOfType<SecondScene>();
         if (nextScene != null)
         {
             nextScene.OnLoadNextLevel -= OnLoadNextLevelHandler;
             nextScene.OnLoadNextLevel += OnLoadNextLevelHandler;
         }
         var lastScene = GameObject.FindObjectOfType<ThirdScene>();
         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!

loadscenes.png (34.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Tomer-Barkan · Dec 24, 2013 at 04:16 PM

The Null Reference exception is probably because cs_finalWindow is null. You're setting it to reference an object in the current scene, but as soon as you switch scene, that object (the main camera) is destroyed, and you can no longer use it. I suggest you move the script FinalGUIWindow from the main camera object, to be in the same object as the reference manager, since this object is not destroyed when you switch scenes.

The ArgumentOutOfRangeException is caused because the list scenes is empty. The OnLoadNextLevelHandler() is still called after you remove all the scenes from the list, so it causes this exception. The solution is to check whether it's empty:

 private void OnLoadNextLevelHandler()
 {
     if (scenes.Count > 0) {
         int randomIndex = Random.Range(0, scenes.Count);
         int level = scenes[randomIndex];       
         scenes.RemoveAt(randomIndex); // Removes the level from the list
         Application.LoadLevel(level);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tomer-Barkan · Dec 24, 2013 at 06:15 PM 1
Share

Try to add a Debug.Log before removing a scene from the list, see why it happens when it shouldn't. Also make sure it starts out with the correct amount of scenes in it, so maybe print out how many scenes in the list before removing one.

avatar image
0

Answer by tom_eberhard · Dec 24, 2013 at 05:35 PM

It's architected a bit strangely, and one of the problems is (probably) that you're removing scenes at randomIndex.

I would architect this a bit differently: Keep a sceneIndex variable, and randomize the scene list before going through the levels.

Also, I'm not sure what you're trying to do with these lines:

defaultScene.OnLoadNextLevel -= OnLoadNextLevelHandler; defaultScene.OnLoadNextLevel += OnLoadNextLevelHandler

Anyway, here's something to get you going in (hopefully) a better direction:

public class LevelManager : MonoBehaviour {

 protected const int MAX = 12;
 
 private int[] scenes;
 private int sceneIndex;

 // Use this for initialization
 void Start () {

     Debug.Log ("LevelManager: Hello");

     scenes = new int[MAX];
     FillList(scenes);
     PrintList (scenes);

     RandomizeList (scenes);
     PrintList (scenes);
     
     sceneIndex = 0;
     OnLoadNextLevelHandler ();
     
     Debug.Log ("LevelManager: Bye");
 }
 
 // Update is called once per frame
 void Update () {
     if (sceneIndex == MAX-1)
     {
         //cs_finalWindow.showCompletedWindow();
     }
 }

 private void FillList(int[] list)
 {
     Debug.Log ("FillList: Hello");
     for (int i=0; i<MAX; i++) {
         list[i] = new int();
         list[i] = i;
     }
     Debug.Log ("FillList: Bye");
 }

 /// <summary>
 /// Randomizes the list, but keeps the first and last element in place (the First Scene and the Last Scene)
 /// </summary>
 /// <param name="list">List.</param>
 private void RandomizeList(int[] list)
 {
     int temp;
     int randomIndex;

     Random.seed = 32192;

     for (int i=1; i<list.Length-1; i++) {
         randomIndex = (int)Mathf.Floor(Random.Range (1,list.Length-2));

         // swap list[i] and list[randomIndex]
         temp = list [i];
         list [i] = list [randomIndex];
         list [randomIndex] = temp;
     }    
 }
 
 private void PrintList(int[] list) {

     Debug.Log ("Printing List");
     string s = "";
     for(int i=0; i<MAX;i++)
     {
         s = s + list[i].ToString("0") + ",";
     }
     Debug.Log ("list.Count = " + list.Length.ToString ());
     Debug.Log (s);
 }
 
 
 private void OnLoadNextLevelHandler()
 {
     //Application.LoadLevel(sceneIndex);
     sceneIndex++;
 }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mmangual_83 · Dec 24, 2013 at 05:43 PM 0
Share

Random.seed = 32192; what does this do???

avatar image tom_eberhard · Dec 24, 2013 at 11:46 PM 0
Share

it's not really necessary, I had used it to debug the RandomizeList() function so that I would get the same random sequence each time. Remove it, and then you'll get a different sequence each time.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers

How to loop an array of 2d sprites 1 Answer

C# how to create a Descending GUI List 1 Answer

Make HUD to show object direction 0 Answers

C# How to Drag and Scale with Mouse Window 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges