Search Issue Tracker

By Design

Votes

0

Found in

4.6.0b9

Issue ID

611272

Regression

No

Resizing a MyClass[] array in the inspector does not make the new element have the class default values, but 0 instead

Scripting

-

To reproduce:
1. Open the attached project. It contains a script with a serializable class that has 3 fields (bool int and float). All of them have some kind of default value. The script also creates two arrays of the class (one that is initialized with new, and one that is not initialized)
2. Attach the script to the main camera
3. Notice that the initialized arrays fields have the default values. If you increase the size of the initialized array the new elements also have the default values.
4. The uninitialized data array is by default size 0; Change the size to 1 - note that the fields do not have the default values

EXPLANATION:
Internally, when an array is resized, the core allocates a zero-initialized memory block big enough to contain the array elements. For performance reasons, this block of data is left uninitialized unless the original size was different than zero: in this case the last element's data is copied onto the newly created elements without invoking any constructor.
The behaviour for non-array fields is different, as long as the constructor gets invoked when they are initialized the first time, and by invoking the constructor the fields get initialized to their default values.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.