- Home /
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?
$$anonymous$$ost of the base and child classes are ScriptableObjects that are created with CreateInstance. Does Unity have a limit to how much or how often you can save these ScriptableObjects? I am getting a lot of dropped data that occurs randomly but most often when the user enters play mode. I can't seem to figure this one out. This bug is slowing down my development process and I would be grateful for a solution.
Answer by rutter · May 08, 2012 at 05:24 PM
Have you been calling `EditorUtility.SetDirty()` on objects you change?
Actually, I am indeed calling EditorUtility.SetDirty often. This is because I need to refresh OnDrawGizmos. If that is the problem, is there another way to force refresh gizmos without losing data?
It's actually good that you're calling that (sorry, should have been more clear). People tend to overlook it, and then the editor won't be aware that you've made changes.
Off the top of my head, I can't think of what might cause your problem. I'll reply back if I think of anything else.
Answer by Silicon · May 08, 2012 at 08:44 PM
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.
I'm not using a custructor with the objects that are being saved. I don't believe this applies.
Your answer
