How to access a list created on EditorWindow script?

Imagine that I have a simple list on my EditorWindow script:

[SerializeField]
public List<int> intValues;

onGui I have a button that adds elements to the list:

if (GUILayout.Button("Add Value", GUILayout.Width(115), GUILayout.Height(20)))
{
    intValues.add(1);
}

My question is: How do I get access to the updated list from another script, Monobehaviour?

This is what I have tried on Monobehaviour script:

serializedObject.FindProperty("intValues");

However:

serializedPropertyMyInt.arraySize

is 0. The only way that works is declaring an array like this:

 Array1 =  new[] { 1, 2, 3, 4, 5 };

on EditorWindow script and call it on Monobehaviour using the same code:

serializedObject.FindProperty("Array1");

But If I update, simple doesn’t work. Using “toArray()” on onGUI didn’t work either. I need to work with those values inside the Monobehaviour script Update function.

Any ideias?

I found out an alternative. Instead of declare the list on EditorWindow script, I declared on Monobehaviour script the same way:

[SerializeField] public List<int> intValues;

This way, I can use it, updated, whatever I want, by first using the reference to the Monobehaviour script (MyBehaviour.cs):

[SerializeField] private MyBehaviour _scriptReference;
_scriptReference = FindObjectOfType(typeof (MyBehaviour)) as MyBehaviour;

and then, point to the List:

[SerializeField]_scriptReference.intValues

If I add values through this, they will be updated when the Monobehaviour script is used. In my case, I used the script on a GameObject and it worked.

I am still curious about how to do it by declaring it on EditorWindow script.

An EditorWindow isn’t meant to be used to get data to persist within the instance, rather they are used to access and manipulate data that is stored outside of the window. So the data should reside in a MonoBehaviour (as you did above), in ScriptableObject asset, Player/Editor Preferences, or some other serialized file (.txt, .json, .xml, etc).

1 Like

Hey there,

The reason you could not do it the first way is because the Runtime Assembly (the code not in editor folders) knows nothing about the Editor Assembly (The code in Editor folders). The Editor Assembly however knows everything about the Runtime Assembly. Their relationship is one directional which it has to be or you would have circular dependency. Circular dependency is a problem for the compiler as it can’t figure out what to compile first since they both require each other to build.

  • You can still do this with reflection but it’s not suggested.
3 Likes