greyBow
greyBow

Reputation: 1348

NullReferenceException when creating folder structure

I'm having trouble creating a directory structure on initial loading of my game. I have a script that is supposed to create a set of folders if they do not exist, but I'm getting error: NullReferenceException: Object reference not set to an instance of an object Loader.Start () (at Assets/_Scripts/Managers/Loader.cs:22) I'm creating an array of the folders that I would like to create and then using a foreach to cycle through the array and using Directory.CreateDirectory(path) it should be creating the directory, but it's not. What am I doing incorrectly here?

Loader.cs

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

public class Loader : MonoBehaviour
{
    public GameObject gameManager;

    // File System List of Folders
    private List<string> folders;

    private void Awake ()
    {
        if (GameManager.Instance == null)
            Instantiate(gameManager);
    }

    private void Start()
    {
        // Create folder List
        folders.Add(Application.persistentDataPath + GameManager.animalDataFilePathRoot);
        folders.Add(Application.persistentDataPath + GameManager.animalDataFilePathJSON);
        folders.Add(Application.persistentDataPath + GameManager.animalDataFilePathTex);
        folders.Add(Application.persistentDataPath + GameManager.animalDataFilePathTemp);

        // If a folder doesn't exist, create it.
        foreach (string folder in folders)
        {
            CreateDirectory(folder);
        }
    }

    // Create Directory
    public void CreateDirectory(string path)
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
            Debug.Log(path + " folder created.");
        }
        else if (Directory.Exists(path))
        {
            Debug.Log(path + " folder already exists.");
        }
    }
}

Variables in GameManager are set up like so:

public static string animalDataFilePathRoot { get; set; }
    public static string animalDataFilePathJSON { get; set; }
    public static string animalDataFilePathTex { get; set; }
    public static string animalDataFilePathTemp { get; set; }
    public static string animalDataFileNameJSON { get; set; }
    public static string animalDataFileNameTex { get; set; }

private void Start()
    {
        InitGameVariables();
    }

 void InitGameVariables()
    {
        animalDataFilePathRoot = "/animalData";
        animalDataFilePathJSON = "/animalData/json";
        animalDataFilePathTex = "/animalData/tex";
        animalDataFilePathTemp = "/animalData/tmp";
        animalDataFileNameJSON = "/animal.json";
        animalDataFileNameTex = "/animalTexture.png";
    }

Upvotes: 0

Views: 392

Answers (1)

Programmer
Programmer

Reputation: 125245

Initialize the folders variable before using it.

private List<string> folders = new List<string>();

Or do that in the Start function:

private List<string> folders;

private void Start()
{
    //Init
    folders = new List<string>();

    // Create folder List
    folders.Add(Application.persistentDataPath + GameManager.animalDataFilePathRoot);
    ...
    ...
}

Also, since you are using the variables from the GameManager class, in the Start function of the Loader class, it would make sense to initialize all those variables in the GameManager class inside the Awake function instead of the Start function.

If you don't, animalDataFilePathJSON and other similar variables may not be initialized at the time they are used.

void Awake()
{
    InitGameVariables();
}

Upvotes: 4

Related Questions