- Home /
Problem with List
So Im calling a function in a class called PathNode, of an instantiated object. I later then call that same class but a different function that sets a value from that list. However im testing the count of the list in this function and getting 0, because im getting null reference. Not sure what im doing wrong!! Please help!!
//AIUpdate Class
public List<PathNode> NodeList;
public GameObject Node;
public PathNode testNode;
void Start ()
{
NodeList = new List<PathNode>();
}
void Update()
{
testNode = NodeList[0];
testNode.setMoverStart();
}
public void createNodeGrid()
{
int val = 0;
for(int i=0;i<=2000;i+=200)
{
for(int k=0;k<=2000;k+=200)
{
PathNode tmp;
tmp= (Instantiate(Node, new Vector3(i, 20, k), Quaternion.identity) as GameObject).GetComponent<PathNode>();
NodeList.Add(tmp);
}
}
createLinks();
}
public void createLinks()
{
int index = 0;
NodeList[index].addLinkedNode(NodeList[8].gameObject);
index++;
NodeList[index].addLinkedNode(NodeList[7].gameObject);
index++;
NodeList[index].addLinkedNode(NodeList[6].gameObject);
index++;
NodeList[index].addLinkedNode(NodeList[5].gameObject);
index++;
NodeList[index].addLinkedNode(NodeList[5].gameObject);
index++;
NodeList[index].addLinkedNode(NodeList[3].gameObject);
index++;
NodeList[index].addLinkedNode(NodeList[2].gameObject);
index++;
NodeList[index].addLinkedNode(NodeList[1].gameObject);
index++;
NodeList[index].addLinkedNode(NodeList[0].gameObject);
index++;
}
//PathNode Class
public List<GameObject> linkedNodes;
void Start () {
linkedNodes = new List<GameObject>();
}
public void setMoverStart()
{
Debug.Log(this.linkedNodes.Count);//get 0
}
public void addLinkedNode(GameObject x)
{
this.linkedNodes.Add(x);
Debug.Log(this.linkedNodes.Count);//get the true size
}
Comment
Your answer
Follow this Question
Related Questions
How do I save and retrieve ordered data? Can I do this with an array? 1 Answer
Limiting size of data structures in editor 1 Answer
A node in a childnode? 1 Answer
List Problem 1 Answer
Need help with converting an array to a list in C#` 4 Answers