NullReferenceException: Object reference not set to an instance of an object
[System.Serializable]
public class allwc {
int dx = 2;
List<wc> lst;
public allwc(List<GameObject> allworker) {
List<wc> lst = new List<wc>();
for (int i = 0; i < allworker.Count; i++) {
wc wcone = new wc();
wcone.x = allworker[i].transform.position.x;
wcone.y = allworker[i].transform.position.y;
wcone.food = allworker[i].GetComponent<worker1>().food;
wcone.mas = new int[3];
lst.Add(wcone);
}
}
public List<wc> getallwc() {
return lst;
}
public int getdx()
{
return dx;
}
}
public void savegame() {
print("save");
allwc allwcOne = new allwc(allworker);
List<wc> lst = new List<wc>();
lst = allwcOne.getallwc();
print(allwcOne.getallwc()[0].x);
}
print(allwcOne.getallwc()[0].x) - where is the mistake ?
Comment
Решение такое заменил List lst = new List(); на lst = new List(); видимо он создает внутри функции локальную переменную и не записывает значение в поле класса.
Your answer
Follow this Question
Related Questions
Class with a List of Classes containing a List of Classes 1 Answer
How to create a class and save a list of that object? 0 Answers
NullReferenceException : Object reference not set to an instance of an object? 0 Answers
How do I modify variables from a different class that belongs to the same C# file? 1 Answer
How to add Script1.List.Class to Script2.List.Class? 0 Answers