Properties don't serialize when class implements interface containing properties

Issue #362 resolved
Gabriel Acosta
created an issue

It took me a little while to track this one down, but if you make an interface that contains a property and then make a class that implements that interface, the property from the interface doesn't serialize, it resets when you hit play. But if the same property is not part of the interface, it works fine.

Attach TestScript to a gameobject and set myProperty and isCollected to true in the inspector. When you hit play, myProperty will still be true, but isCollected (which is part of the interface) will reset to false. But if you edit TestScript so it doesn't implement ITestInterface, isCollected will now remain true as expected.

Using Unity 2017.2.0f3, Windows 7

using Sirenix.OdinInspector;
using Sirenix.Serialization;

public class TestScript : SerializedMonoBehaviour, ITestInterface
{
    [OdinSerialize, ShowInInspector]
    public bool myProperty { get; set; }//this works fine

    [OdinSerialize, ShowInInspector]
    public bool isCollected { get; set; }//this resets when you hit play
}

public interface ITestInterface
{
    [OdinSerialize, ShowInInspector]
    bool isCollected { get; set; }
}

Comments (5)

  1. Mikkel Dockweiler Sørensen

    This is a deliberate design decision in the current version of Odin. The Odin Serializer refuses to serialize any non-auto properties. This is to prevent any code running before an object has been fully constructed by the deserializer, and helps to avoid errors.

    Any properties you implement from an interface are always virtual even if they look like an auto-property.

    We have recently changed this to allow for the option of serializing non-auto properties, but this is not currently available in Odin Inspector.

  2. Log in to comment