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
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.
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- ArgumentOutOfRangeException when manipulating the Project Settings search bar
- [Linux] Touch freezes in Player when any mouse button is clicked
- UnityEngine.Rendering.Volume.OnEnable() is called twice before OnDisable() creating multiple copies of the same volume when a volume is enabled right after Domain Reload when entering Play Mode
- Hinge Joint transforms are reset when toggling the "useMotor" property
- Motion Vector texture reports incorrect Mesh data when Mesh is coming from off-screen
Add comment