- Home /
Other
List is not null with [SerializeField]
Hi there,
I just had a very weird behaviour with one of my Lists. I need to know if one of my lists is null. So I test that with
void Start()
{
Debug.Log(myList == null);
}
When I define my variable like this:
[SerializeField] List<MyObjects> myList = null;
The console output is: False
But when I define my variale like this:
List<MyObjects> myList = null;
The console output is: True
That's really dangerous and confusing, any idea why that is the case?
Answer by bpaynom · Feb 24, 2019 at 08:03 PM
When you go to the editor, those objects which have attached your script modify that list and convert it to an empty list. Not null, list of size 0.
As @Bunny83 pointed out , Serialization does not support null values.
HideInInspector does not change the serialization behaviour. As long as the variable is serialized, it will contain a list instance. Custom serializable classes as well as Lists or arrays do not support null values as they are serialized inline by value. Have a look at script serialization
I didn't know that, thanks for the clarification!
Since you closed the question i took the liberty of accepting this answer.
Follow this Question
Related Questions
[SerializeField] private Vector3[] array works in editor/doesn't work in stand-alone build 1 Answer
If I have a bool method, how can I see in the inspector whether it returns true or false? 2 Answers
Managing a large list of objects 1 Answer
Name-fetching script working but with error. 1 Answer
adding components to a gameObject in a list of lists 2 Answers