EditorGUILayout.ObjectField losing object ref when no longer focus on the gameobject where the script is

Hey.

When you click out of the gameobject (like click to another object) where the script is it loses the reference to the selected object. Im probably missing something very simple but cant get it to work or is supposed to work like this? Sorry if my explanation isn’t great. Code atm:
if (source = EditorGUILayout.ObjectField (source, typeof(GameObject), true)){
spawner.wantedSpawn = (GameObject)source;
}

So this works but when you select for example another gameobject the reference is lost.

Hey. In case someone else is wondering i did find the solution. So instead of using target.wantedSpawn(directly accesing the script) you need to use serialized properties. Dont do this

public override void OnInspectorGUI()
    {
	spawnArray spawner = (spawnArray)target;
	
	source = EditorGUILayout.ObjectField(source, typeof(GameObject), true);
            if(source){
                spawner.wantedSpawn = (GameObject)source;
            }
	}

Do this

 SerializedProperty wantedSpawn;
        private void OnEnable()
        {
            //Setup all the properties. serializedObject represent the object being inspected
            wantedSpawn = serializedObject.FindProperty("wantedSpawn");//this is game object in my case
    		useMirror = serializedObject.FindProperty("useMirror");//this is bool in my case
        }
    	public override void OnInspectorGUI()
        {//Update the serializedProperty first thing to do
    		serializedObject.Update();
    		//now just use propertyfield to change it to what ever. propertyField is used for every type
    		EditorGUILayout.PropertyField(wantedSpawn);
    		EditorGUILayout.PropertyField(useMirror);
    		//apply changes. last thing to do
    		serializedObject.ApplyModifiedProperties();
    	}