2017-07-31 5 views
0

ゲームの初期ロード時にディレクトリ構造を作成できません。存在しない場合にフォルダのセットを作成するはずのスクリプトがありますが、エラーが発生しています:NullReferenceException: Object reference not set to an instance of an object Loader.Start() (at Assets/_Scripts/Managers/Loader.cs:22)作成したいフォルダの配列を作成してから、foreachを使用して循環させます配列を使用してDirectory.CreateDirectory(path)を使用してディレクトリを作成する必要がありますが、そうではありません。私はここで間違って何をしていますか? GameManagerでフォルダ構造の作成時にNullReferenceExceptionが発生する

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."); 
     } 
    } 
} 

変数がそうように設定されています

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"; 
    } 
+0

Loader-Objectのインスタンスを初期化していないようです。 var loader = new Loader();のように記述します。 loader.Start(); – kassi

+0

また、フォルダ変数を新しいリストで初期化します。 – kassi

答えて

4

は、それを使用する前にfolders変数を初期化します。

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

または実行スタート機能でその:

private List<string> folders; 

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

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

また、あなたがLoaderクラスのStart機能で、GameManagerクラスから変数を使用しているので、それはなるだろうStart関数の代わりにAwake関数内のGameManagerクラスのすべての変数を初期化することができます。

そうでない場合、animalDataFilePathJSONと他の同様の変数は、使用時に初期化されないことがあります。

void Awake() 
{ 
    InitGameVariables(); 
} 
関連する問題