I’m trying to create instance of class using List instead of array and getting error ,
i don’t know the exact cause
please let me know if there is any workaround to use List of Class objects
Thanks in advance for any suggestion or code !
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable] public class BaseClass{
public int index ;
public List<GameObject> itemList;
}
public class GameLogicClass : MonoBehaviour {
public List<BaseClass> baseClassInstance;
void Start(){
for(int _m = 0;_m < 10;_m++){
baseClassInstance.Add(new BaseClass());
baseClassInstance[_m].index = 1;
}
Debug.Log(baseClassInstance[0].itemList.Count ); // < Getting ERROR here : NullReferenceException: Object reference not set to an instance of an object GameLogicClass.Start ()
}
}
Oh looks like someone beat me to doing the reformatting for you! :D
– Scribei just edited my post , could you please have a look , it went bad because of hitting login/post button before completing my question :-P
– chetanisinanandThanks Scribe for your quick reply, I just tried your suggestion and initialised list inside Start(), however i'm still getting the same error Note: I'm able to print/overwrite "index" value , the only problem is with "itemList"
– chetanisinanandYou need to initialise that as well! On my phone so sorry for any bad formatting public List<GameObject> itemList = new List<GameObject>(); Or by doing: baseClassInstance.Add(new BaseClass()); baseClassInstance[_m].index = 1; baseClassInstance[_m].itemList = new List<GameObject>();
– Scribe