Accessing class properties through getters & setters in editor mode

Hi!

I'm trying to set class variables through getters & setters. I wrote a custom editor that exposes these methods. The code works just fine, that is if I change values while the game is in play mode, these values are passed to the respective variables. However if I set the values in edit mode, the values are lost as soon as I click on the Play button (values are actually set to the default). I've added the @script ExecuteInEditMode() but to no avail! How can persist the changes I've made in the custom editor while in edit mode?

Thanks!

Code for class SpriteController:

@script ExecuteInEditMode()

private var columns : int;

//Getters & setters
function SetColumns(value : int){
    if(value < 0){
        value = 0;
    }

    columns = value;
}

function GetColumns() : int{
    return columns;
}

Code for SpriteControllerEditor:

@CustomEditor (SpriteController)

class SpriteControllerEditor extends Editor {

function OnInspectorGUI () {

    target.SetColumns(EditorGUILayout.IntField("Columns", target.GetColumns()));
    if (GUI.changed)
        EditorUtility.SetDirty(target);
}

}

[EDIT: Ooops, just learned something new - in fact, there's an attribute for that: SerializeField. So, you could do

@SerializeField
private var columns : int;

and it should work.]

Private member variables are not persisted by Unity [unless you use "SerializeFiled", see above]. So you'll have to write:

public var columns : int;

Personally, when I have public member variables that I only have for "technical reasons", I make sure I don't call them from outside the class where they are defined because it's lower case (and "lower-case" means "private" to me, upper-case means "real public properties").

Unfortunately, the way Unity is designed, you have to make any state variables that should be persistent public; so you need your own conventions to keep things clean and tidy (like the "lower case means private").

The reason "lower case means private" works very naturally for me is due to the C# background and the availability of "real properties":

public int Columns {
    get { return columns; }
    set {
        if (value < 0) {
            value = 0;
        }
        columns = value;
    }
}

Those are always uppercase (at least if you follow common conventions).