From 3feeec1649929726d37aad54aa5a35eb2b5adbec Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 6 Feb 2022 22:44:40 +0100 Subject: [PATCH] Fix structure initialization with default field value if attribute has different value HasInvalidPathChar #688 --- Source/Editor/Utilities/Utils.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Source/Editor/Utilities/Utils.cs b/Source/Editor/Utilities/Utils.cs index d85a06296..9ec08aacc 100644 --- a/Source/Editor/Utilities/Utils.cs +++ b/Source/Editor/Utilities/Utils.cs @@ -829,7 +829,13 @@ namespace FlaxEditor.Utilities var attr = field.GetAttribute(); if (attr != null) { - field.SetValue(obj, attr.Value); + // Prevent value type conflicts + var value = attr.Value; + var fieldType = field.ValueType.Type; + if (value != null && value.GetType() != fieldType) + value = Convert.ChangeType(value, fieldType); + + field.SetValue(obj, value); } else if (isStructure) { @@ -845,7 +851,13 @@ namespace FlaxEditor.Utilities var attr = property.GetAttribute(); if (attr != null) { - property.SetValue(obj, attr.Value); + // Prevent value type conflicts + var value = attr.Value; + var propertyType = property.ValueType.Type; + if (value != null && value.GetType() != propertyType) + value = Convert.ChangeType(value, propertyType); + + property.SetValue(obj, value); } } }