No, access modifiers nothing have to do with variable initialization in this case.
The fact that only when public it shows the warning is caused by the fact that if you have a public field (or a SerializedOne) you can assign it by the Editor, but if it's private, you can't, so the Engine is warning you about the default value.
If you make your list private, but you initialize the variable like:
private List<GameObject> Onbelt = null;
or
private List<GameObject> Onbelt = new List<GameObject>();
The warning will disappear, cause you are forcing the default value.
EDIT:
To be clear, in c# access modifiers nothing have to do with variable initialization, but in UNITY EDITOR the fact that having a public variable, let's the user fill this list, so by default, is created with new List() instead of default null.
So this won't work (NullReferenceException
):
private List<int> list;
void Start()
{
list.Add(2);
}
But this WILL work:
public List<int> list;
void Start()
{
list.Add(2);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…