Inspector automatically fills members of reference type?

Hi,
I have a script component which has a public field with reference type(e.g. class), and the value of this field is null by default. I found that if I select a object with this component in the editor thus making its content visible in the Inspector, the Inspector automatically fills the public field with a instance of the type of the field, and this instance seems created using its default constructor.

I also tried not to select the object and print out the value of this field, then it was kept null as I expected.

I think it is strange and would like to know is this the default behavior of the Inspector?

Thanks.

1 Answer

1

No it does not not in the general case: Reference type are kept to default value null (shown as ‘None’ is the inspector, when it is derived from Component, not shwon at all otherwise except for Serializable classes, see below).

The only exception concerns Serializable classes. For those a default instance is created so that you can fill its members. This behaviour is recursive: if the serializable class also has a reference type member which in turn is serializable…

Oh, yes, I did make the type of that field Serializable. Thanks for answering, it really helps.