- Home /
Nested Class Initialization
I have been getting Nullreference error when I try
NodeList[lcv].MasterNode == TestNode
in the NodeManager class.
The classes in use
public class NodeManager : MonoBehaviour {
private static int InitialListSize = 100;
public NodeAccountant[] NodeList = new NodeAccountant[InitialListSize];
public int NodeCount = 0;
public class NodeAccountant {
public Node MasterNode = new Node();
public int[] Neighbors = new int[10];
public int NieghborCount = 0;
}
// Use this for initialization void Start () { for(int lcv = 0; lcv < NodeList.Length; lcv++) { NodeList[lcv] = new NodeAccountant(); NodeList[lcv].NieghborCount = 0; }
}
....
}
public class Node : MonoBehaviour {
public string Team {get; private set;}
public int NodeIDNum;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void GenerateNode(string TeamName)
{
Team = TeamName;
}
public bool IsAlive()
{
return(GetComponentInChildren() != null);
}
}
I know MonoBehavior classes freak out when you use new so I was wondering how do I resolve this issue.
Any help would be greatly appreciated.
On edit added more to NodeManager.
Answer by Bunny83 · Jan 06, 2013 at 01:05 AM
You don't create any instance of your "NodeAccountant" class. All you've done is creating an array with 100 references which are all null. "Normal" classes have to be created with "new".
Your next problem is that you try to create an instance of your Node class with "new". Components (classes derived from MonoBehaviour are components too) can't be created with new. Components can only be created with AddComponent.
You might want to rethink your whole concept. I can't give you more instructions since we don't know the actual purpose of those classes.