Trouble with EditorGUI

I’m having some trouble with EditorGUI/EditorGUILayout. I’ve got a setup where I want the user to be able to edit the variables of an object with an Inspector-like interface in an EditorWindow. So, I thought it a good idea to use FloatField, IntField and TextField for the variables. But the problem is that when the user selects a new object, all the fields change to show the values of the new object, except the field that has focus! It still shows the value of the previous object.

Is this a bug, or am I doing something wrong?

Here’s the object I’m trying to edit:

using System;
using UnityEngine;

public class TestClass : MonoBehaviour
{
	public string firstName;
	public string lastName;
	public int age;
	public float weight;
}

And here’s the code for the EditorWindow:

using System;
using UnityEditor;
using UnityEngine;

public class Editor : EditorWindow
{
	[MenuItem ("Custom/Editor")]
	static void Edit ()
	{
		Editor editor = new Editor();
		editor.position = new Rect(200, 200, 600, 500);
		editor.autoRepaintOnSceneChange = true;
		editor.Show();
	}

	void OnGUI()
	{
		if (Selection.activeGameObject != null)
		{
			TestClass selected = (TestClass)Selection.activeGameObject.GetComponent(typeof(TestClass));
			if (selected != null)
			{
				selected.firstName = EditorGUILayout.TextField("First Name", selected.firstName);
				selected.lastName = EditorGUILayout.TextField("Last Name", selected.lastName);
				selected.age = EditorGUILayout.IntField("Age", selected.age);
				selected.weight = EditorGUILayout.FloatField("Weight", selected.weight);
			}
		}
	}
}

Any help appreciated. Thanks.

Alright, now it’s getting really weird. Seeing that the problem only occurs when a field has keyboard focus, I tried defocusing all fields when the selection changes (using GUIUtility.keyboardControl = 0). This makes all fields change to the values of the new selection. So far, so good. But then, when I click in the field that previously had focus, it changes back to the value of the previous selection. WTF?!

I’m starting to think the case is less me doing something wrong, and more EditorGUI being horribly broken. (The standard GUI.TextField doesn’t behave this way.)

Can anyone with the 2.5 beta try this out and see if it’s been fixed? Otherwise I’m gonna report it as a bug.

Did you ever hear anything back from this? I’ve been running into this same problem all day - finally found this post. Oddly enough, forcing keyboardControl would not fix it for me (didn’t have any effect - text remains highlighted, cursor remains in text field).

Switching from EditorGUILayout.TextField to GUILayout.TextField gave me the result I was looking for - it’s got to be something wrong with the EditorGUILayout version.

Edit: To be clear, none of the EditorGUI text controls seem to update properly - not just TextField.