- Home /
List Problem
So I have function that simply is suppose to add stuff to it however when i test it after in the function im getting a null object?!?!?!? Please help!! from the log i get null then 121.
public List<PathNode> NodeList;
public int size;
public GameObject Node;
public void createNodeGrid()
{
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 PathNode;
NodeList.Add(tmp);
}
}
Debug.Log(NodeList[0]);
size = NodeList.Count;
Debug.Log(size);
}
Comment
Did you attach the node to the game object variable in the editor??
Best Answer
Answer by whydoidoit · Feb 11, 2014 at 06:34 AM
You get back what you instantiate - you can't just use "as" on it. Use GetComponent.
tmp= (Instantiate(Node, new Vector3(i, 20, k), Quaternion.identity) as GameObject).GetComponent<PathNode>();
Whatever type of variable you are holding, you get back from Instantiate - so in your case a GameObject.
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
Problem with List 0 Answers
Need help with converting an array to a list in C#` 4 Answers