0

The following code will get NullReferenceException error when I call LoadLevels() in another script or in Unity:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
using UnityEngine;
using System;

public class LoadCustomLevels : MonoBehaviour
{
    static LoadCustomLevels instance;
    public GameObject ButtonPrefab; // this object referenced in Unity
    public Sprite DefaultPNG; // this object referenced in Unity

    private void Awake()
    {
        instance = GetComponent<LoadCustomLevels>();
    }
    void LoadTheLevels()
    {
        string path = Application.persistentDataPath + @"\Levels";
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        string[] levelPaths = Directory.GetDirectories(path);
        foreach (string item in levelPaths)
        {
            if (File.Exists(item + @"\data") && File.Exists(item + @"\objects"))
            {
                GameObject currentButton = Instantiate(ButtonPrefab, transform);

                currentButton.transform.GetChild(0).gameObject.GetComponent<Text>().text = new DirectoryInfo(path).Name;

                currentButton.transform.GetChild(1).gameObject.GetComponent<Image>().sprite = DefaultPNG;
                if (File.Exists(item + @"\icon.png"))
                {
                    Texture2D levelImagetex = new Texture2D(700, 500);
                    levelImagetex.LoadImage(File.ReadAllBytes(item + @"\icon.png"));
                    currentButton.transform.GetChild(1).gameObject.GetComponent<Image>().sprite = Sprite.Create(levelImagetex, currentButton.transform.GetChild(1).GetComponent<Rect>(), new Vector2(0.5f, 0.5f));
                }

                string version = File.ReadAllBytes(item + @"\data").ToString().Split(',')[0];
                if (Application.version != version)
                    currentButton.transform.GetChild(2).gameObject.GetComponent<Text>().color = Color.red;
                currentButton.transform.GetChild(2).gameObject.GetComponent<Text>().text = version;

                long levelSize = 0;
                foreach (var item2 in Directory.GetFiles(item))
                {
                    levelSize += new FileInfo(item2).Length;
                }
                levelSize /= 1024;
                currentButton.transform.GetChild(3).gameObject.GetComponent<Text>().text = levelSize.ToString() + "Kb";

                if ((DateTime.Now - new DirectoryInfo(item).LastAccessTime).Minutes <= 60)
                    currentButton.transform.GetChild(4).gameObject.GetComponent<Text>().text = (DateTime.Now - new DirectoryInfo(item).LastAccessTime).Minutes + " minute(s) ago" ;
                else if ((DateTime.Now - new DirectoryInfo(item).LastAccessTime).Hours <= 24)
                    currentButton.transform.GetChild(4).gameObject.GetComponent<Text>().text = (DateTime.Now - new DirectoryInfo(item).LastAccessTime).Hours + " hour(s) ago";
                else
                    currentButton.transform.GetChild(4).gameObject.GetComponent<Text>().text = (DateTime.Now - new DirectoryInfo(item).LastAccessTime).Days + " day(s) ago";
            }
        }
    }

    public static void LoadLevels()
    {
        instance.LoadTheLevels(); //error
    }
}

I also try instance = this; in Awake void but it's didn't work too

My goal is to call LoadTheLevels() elsewhere without static it(include I newbie in programming).

  • 1
    Are you sure `instance` is set => Is there a `LoadCustomLevels` active and enabled in your scene? Is `Awake` called before `LoadLevels`? – derHugo Oct 29 '21 at 15:46
  • 1
    And you know in general instead of `instance = GetComponent();` you rather want to use `instance = this;` ;) – derHugo Oct 29 '21 at 15:46

0 Answers0