#Btw I need not the string value but the
1 messages · Page 1 of 1 (latest)
So I need to use another approach, not the property drawer?
I need to add a new instance to the list by clicking the button.
You can do that through SerializedProperty (and you should)
SerializedProperty array = property.FindPropertyRelative("StingsList");
SerializedProperty newElement = array.InsertArrayElementAtIndex(array.arraySize);
newElement.stringValue = "whatever";
And then don't forgot to call ApplyModifiedProperties on the SerializedObject.
InsertArrayElementAtIndex returns void.
I'm trying like this:
property.FindPropertyRelative("StatModifiers").InsertArrayElementAtIndex(0);
DamageModifier elem = property.GetArrayElementAtIndex(0) as DamageModifier;
elem = new DamageModifier();
but it says annot convert type 'UnityEditor.SerializedProperty' to 'DamageModifier' via a built-in conversion to my "as DamageModifier" conversion.
How is DamageModifier serialized? As a normal serialized class/struct or with SerializeReference?
And yeah sorry, I forgot it doesn't return the new element. You'll have to add it and then get it with GetArrayElementAtIndex(array.arraySize - 1)
Or 0 if you want to add it to the start
With serializeReference like this
[SerializeReference] public List<IStatModifier> StatModifiers = new();
[Serializable]
public class DamageModifier : IStatModifier
{
[SerializeField] float _damage;
public void Modify(BehaviourParameters parameters)
{
throw new NotImplementedException();
}
}
You can't cast a SerializedProperty to anything. Everything has to be set through its properties.
For SerializeReference, that would be through the managedReferenceValue property.