I've been following along with a simple RTS tutorial, but I've hit some kind of weird problem where the building scriptable objects I've created and placed in the Assets/Resources/ScriptableObjects/Units/Buildings folder are not being loaded at run time. Here is DataHandler.cs
public class DataHandler
{
public static void LoadGameData()
{
Globals.BUILDING_DATA = Resources.LoadAll<BuildingData>("ScriptableObjects/Units/Buildings") as BuildingData[];
}
}
Here is GameManager.cs which is attached to a GAME object in the scene
public class GameManager : MonoBehaviour
{
private void Awake()
{
DataHandler.LoadGameData();
}
}
Here is the Awake method for the UIManager (also attached to the GAME object)
private void Awake()
{
//create texts for each in-game resource (gold, wood, stone)
_resourceTexts = new Dictionary<string, Text>();
foreach (KeyValuePair<string, GameResource> pair in Globals.GAME_RESOURCES)
{
GameObject display = Instantiate(gameResourceDisplayPrefab);
display.name = pair.Key;
_resourceTexts[pair.Key] = display.transform.Find("Text").GetComponent<Text>();
_SetResourceText(pair.Key, pair.Value.Amount);
display.transform.SetParent(resourcesUIParent);
}
_buildingPlacer = GetComponent<BuildingPlacer>();
//create buttons for each building type
_buildingButtons = new Dictionary<string, Button>();
for (int i = 0; i < Globals.BUILDING_DATA.Length; i++)
{
BuildingData data = Globals.BUILDING_DATA[i];
GameObject button = Instantiate(buildingButtonPrefab);
button.name = data.unitName;
button.transform.Find("Text").GetComponent<Text>().text = data.unitName;
Button b = button.GetComponent<Button>();
_AddBuildingButtonListener(b, i);
button.transform.SetParent(buildingMenu);
_buildingButtons[data.code] = b;
if (!Globals.BUILDING_DATA[i].CanBuy())
{
b.interactable = false;
}
}
}
Every time I try to run the game I get this error:
NullReferenceException: Object reference not set to an instance of an object UIManager.Awake () (at Assets/Scripts/UIManager.cs:37)
which directly corresponds to the for loop. Through some experimenting I've seen that the problem is that the contents of the Buildings folder ("House" and "Tower" BuildingData objects) are not being loaded into Globals.BUILDING_DATA.
public static class Globals
{
public static BuildingData[] BUILDING_DATA;
public static int TERRAIN_LAYER_MASK = 1 << 8;
public static List<UnitManager> SELECTED_UNITS = new List<UnitManager>();
public static Dictionary<string, GameResource> GAME_RESOURCES = new Dictionary<string, GameResource>()
{
{"gold", new GameResource("Gold", 500) },
{"wood", new GameResource("Wood", 500) },
{"stone", new GameResource("Stone", 500) }
};
}
Any ideas what could be going wrong? I tried to follow the tutorial as closely as possible to prevent this kind of problem.