I have a class for lists called UniqueListWithActions<T> with the constructor method
public UniqueListWithActions(List<T> _list)
{
list = _list;
}
list is declared as
public List<T> list;
but in the method ClearNulls, the debug returns "null and False".
public void ClearNulls()
{
for (int i = list.Count - 1; i >= 0; i--)
{
Debug.Log(list[i] + " and " + (list[i] == null));
if (list[i] == null)
{
list.RemoveAt(i);
}
}
}
I set up the following, put it on an empty gameobject, put two other empty game objects into the list through the inspector, destroyed one so the reference was missing, and then hit escape and it still comes up "null and False".
public class ListTest : MonoBehaviour
{
public UniqueListWithActions<GameObject> uniqueList = new UniqueListWithActions<GameObject>(new List<GameObject>());
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
uniqueList.ClearNulls();
}
}
}
I don't understand how it can be null enough that "null" is what's returned in the debug but not "null" enough that list[i] != null. It's not called the same frame the object is destroyed since i do it by hand. How can I clear all the null objects in the list if null isn't null?
List<T>.RemoveAll