Cannot modify floatfield value in custom editor window

Hi everyone,

I’m learning how to make my own editor window.

I’ve made one to achieve some specific manipulations over GOs.
When a GO is selected, I need to display its z coordinate (among other properties). Thus I made a floatfield which displays its transform.position.z. Very simple.

The problem is I cannot modify its z position from that field, neither by typing a value, nor by sliding. The value keeps « sticking » to the actual z coordinate.
How can I make the float field interact with the GO (as in the inspector’s Transform component) ?

And a subsidiary question : you’ll notice that to refresh my window, I’m using Repaint(). But I’m not sure where is the best place to call it : is it at the end of OnGUI() ? Or within the Update() ?

Here is my code :

    public GameObject go;

    void OnGUI() {
        // display selected GO
        go = Selection.activeGameObject;
        go = (GameObject)EditorGUILayout.ObjectField("Selected GO", go, typeof(Object), true);
        if (go) {
            // display z pos
            float zPos = go.transform.position.z;
            zPos = EditorGUILayout.FloatField("z position", zPos);
        }

       // Repaint(); // refresh window here ?...
    }

    private void Update() {
        Repaint(); // ... or here ?
    }

Thanx for any help.

Your code does work perfectly because you don’t do anything with the modified value, at all. You create a local variable “zPos” which is modified. However at any time you always read in the z position of the gameobject but you never change the position of the gameobject.

Since transform.position is a property you can not directly modify the z position, even if you had tried. You can only assign the whole Vector3 value at once. So try something like that:

Vector3 pos = go.transform.position;
pos.z = EditorGUILayout.FloatField("z position", pos.z);
go.transform.position = pos; // this is the important line