Unity Sometimes Ignores Changes

I’m working an a complex editor script that involves saving objects of a custom class type. After changing a property in the custom class and then entering play mode, the variable would sometimes revert to its previous value.

Example:
GameObject with component type Animal is in the scene view.
Animal has an int property age with the value of 5.
User changes the value to 8 from the editor script.
User enters play mode and then exits play mode.
The variable would sometimes, but not always, revert back to the value of 5.

What could be some possible reasons why this happens and how can I make sure that no data is lost?

It depends on how to reference the values in the constructor. For instance if the constructor does not make a call to the property but a call to the field, all instances of that constructor will make the call to the reference field. See Example:

public class Animal
{
   private int value;
   public int Value{ get; set;}
   public Animal { value = 0; }
   public int DefaultReturn { return value; }
   public int PropertyReturn {return Value; }
}

Typically the animal constructor will reference value even if you set it to a different level. I’ve just seen where if I’m setting a property you want to return the property and not the private variable. It may just be that I’m not as good with objects as I thought but I have seen DefaultReturn send a different value than PropertyReturn.

Have you been calling EditorUtility.SetDirty() on objects you change?