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
- Unity CIL Linker fails on Player build when persistent listeners have "<" and ">" in their XML attribute names
- ”Lighting data asset ‘LightingData’ is incompatible with the current Unity version…” warnings are thrown when saving Indoors (URP) and Outdoors (URP) Scenes as Scene Templates
- [iOS] The screen blinks when transitioning from custom to Unity splash screen
- [macOS] ”Ignoring depth surface load action as it is memoryless” warnings are thrown when taking Game View Snapshot
- UI Builder Inspector scrolls back up when changes on an expanded but not fully displayed Inspector tab are saved
Add comment