So i'm trying to create a building prototype and adding that to a list. However after i create the building and try to add it to the list I get a null reference error on line 7 of createAllBuildings Might be important, so the createAllBuildings function is in my worldcontroller class and the constructor is in a non monobehavior building data class. Debug.Log(b) = Building But I cannot access any of b's variables. Debug.Log in constructor = this.buildingName: Empty movecost: 1 width: 1 height: 1 NullReferenceException: Object reference not set to an instance of an object WorldController.createAllBuildings () (at Assets/WorldController.cs:185) thanks! Code (CSharp): //Create all buildings public void createAllBuildings() { Building b = new Building("Empty", 1f, 1, 1); Debug.Log(b); Building.BuildingPrototypes.Add("Empty", b); } Code (CSharp): public Building(string objectType, float movecost, int width, int height) { this.buildingName = objectType; this.moveCost = movecost; this.width = width; this.height = height; Debug.Log("this.buildingName: " + objectType + buildingName + " movecost: " + movecost + " width: " + width + " height: " + height); }
Its hard to understand whats going on, is the following a static function: Code (CSharp): Building.BuildingPrototypes.Add("Empty", b); Because if it isnt your calling a function that doesnt exist. a full code of building would be useful to see...