0

I’m developing a casual 2D Game on unity. And I’m a little stuck. I have 2 scripts: one loads when the game starts - (MainMenuScript.cs), one loads when every scene starts - (LevelControlScript.cs)

I need to randomize scenes, but index_scene = UnityEngine.Random.Range(a, b) not quite what I need in my case. But I still need to show scenes in random sequence. I wrote this code where:

MainMenuScript.cs:

  1. Creates a list, where we put played scenes. It should be initialized once on loading.

LevelControlScript.cs:

  1. Chooses the next scene randomly and check it with the values in the list.

  2. If the list includes that scene - choose another random scene, if it’s not in the list - it plays and after that the scene should be added to the list of played scenes.

  3. When all the scenes were played - the list should be cleared.

So, it should rotate until I manually leave the level.

However, I don’t understand why unity shows this error and how to fix it: NullReferenceException: Object reference not set to an instance of an object LevelControlScript.LoadNextLevel () (at Assets/Scripts/LevelControlScript.cs:606)

MainMenuScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenuScript : MonoBehaviour {

    public List<int> remember = new List<int>(); //here

    public void StartLvl()
    {
        SceneManager.LoadScene("Scenes/LVL");
    }

    public void Quit()
    {
        Application.Quit();
    }
}

LevelControlScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelControlScript : MonoBehaviour {

    MainMenuScript mainmenu; //here

    // Variable to contain current scene build index
    int currentSceneIndex;
    void Start() {

        mainmenu = GetComponent<MainMenuScript>(); //here
        
        // Getting current scene build index
        currentSceneIndex = SceneManager.GetActiveScene().buildIndex;    
    }

    // Method is invoked when correct answer is given
    public void RightAnswer()
    {
        Code...    
    }

    // Method loads next level
    public void LoadNextLevel()
    {
        int index_scene = UnityEngine.Random.Range(1, 10);
        foreach (int index in mainmenu.remember)
        {
            if (index == index_scene)
            {
                index_scene = UnityEngine.Random.Range(1, 10);
            }
        }
        if (mainmenu.remember.Count == 10)
        {
            mainmenu.remember.Clear();
        }
        mainmenu.remember.Add(index_scene);
        SceneManager.LoadScene(index_scene);
    }
}
AnBlsv
  • 1
  • Which line exactly is throwing that error? Unity is saying "LevelControlScript.cs:606" i.e line 606, which one is that? Please [edit] your question to highlight the line in question with a either a comment, or a separate code block containing only that line or both – MindSwipe Mar 29 '21 at 11:18
  • Also [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – MindSwipe Mar 29 '21 at 11:19
  • @MindSwipe line 606: foreach (int index in mainmenu.remember) – AnBlsv Mar 29 '21 at 12:07
  • So `mainmenu` is null, have you made sure to add the `mainmenu` component to your GameObject that contains the `LevelControlScript`? – MindSwipe Mar 29 '21 at 12:09
  • @MindSwipe I'm sorry, but I'm pretty new with it... I can't use it like GameObject in Unity. If I add mainmenu to GameObject in my LevelControlScript, like: **public GameObject mainmenu;** I have an error: error CS0102: The type 'LevelControlScript' already contains a definition for 'mainmenu'. – AnBlsv Mar 29 '21 at 12:53

1 Answers1

0

I'm not completely sure if this could help, but since MainMenuScript loads in the in the initial (Menu) scene and is still required in all other scenes) you should probably add DontDestroyOnLoad in a start function in the MainMenuScript.

Edit:

Try this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenuScript : MonoBehaviour {

    public List<int> remember = new List<int>(); //here
    private static bool created = false;

    void Start()
    {
        if (!created)
        {
            DontDestroyOnLoad(this.gameObject);
            created = true;
        }
    }
    public void StartLvl()
    {
        SceneManager.LoadScene("Scenes/LVL");
    }

    public void Quit()
    {
        Application.Quit();
    }
}

This should work, but if it doesn't, try changing LevelControlScript to this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelControlScript : MonoBehaviour {
    GameObject mainmenugameobject;
    MainMenuScript mainmenu; //here

    // Variable to contain current scene build index
    int currentSceneIndex;
    void Start() {
        mainmenugameobject = GameObject.Find("Changethistothenameofthemenugameobject"):
        mainmenu = mainmenugameobject.GetComponent<MainMenuScript>(); //here
        
        // Getting current scene build index
        currentSceneIndex = SceneManager.GetActiveScene().buildIndex;    
    }

    // Method is invoked when correct answer is given
    public void RightAnswer()
    {
        Code...    
    }

    // Method loads next level
    public void LoadNextLevel()
    {
        int index_scene = UnityEngine.Random.Range(1, 10);
        foreach (int index in mainmenu.remember)
        {
            if (index == index_scene)
            {
                index_scene = UnityEngine.Random.Range(1, 10);
            }
        }
        if (mainmenu.remember.Count == 10)
        {
            mainmenu.remember.Clear();
        }
        mainmenu.remember.Add(index_scene);
        SceneManager.LoadScene(index_scene);
    }
}

Hope this helps :)

Fadi
  • 32
  • 4
  • Hello, I believe that it should works, but I don't know how input it in my program correctly, because earlier I did this, but it didn't work – AnBlsv Mar 30 '21 at 13:22
  • Thanks so much! I tried that you wrote and changed it a bit. It works now :) – AnBlsv Apr 01 '21 at 12:43