SerializedProperty Setting the same Path for All List Items

Hi there,

I’m currently stuck while trying to create a custom forest generator tool. I want to create a list of tree types (structs) that I can add and remove using UI Elements (Using OnCreateInspectorGUI() ), that I can then bind to and get the data from when I come to generate the forest.

m_TreeTypes is the Array of structs that I will increment on an “Add” Button press. It is on a monobehaviour script so I’m using “FindProperty” to get access to it. The code below is on a script that inherits from editor

I’ve got the binding to a singular item working fine (snippet from larger script):

         	var treeTypes = serializedObject.FindProperty("m_TreeTypes");
		var item = m_TreeTypesSP.GetArrayElementAtIndex(m_TreeTypesSP.arraySize - 1);

		var soItem = new SerializedObject(item.objectReferenceValue);
		var name = soItem.FindProperty("name");
	
       var scrollContainer = new ScrollView();
        scrollContainer.name = "scroll";
        scrollContainer.style.width = 350;
        scrollContainer.style.height = 400;

        var Treename = new TextField();
        Treename.label = "Tree Name:";
        Treename.value = "Test";
        Treename.bindingPath = $"m_TreeTypes.Array.Data[{m_ListSize - 1}].name";
        Debug.Log(Treename.bindingPath);
        Treename.multiline = false;
        Treename.maxLength = 140;

When increasing the list size with the “add” button however however:

(on click functionality)

			m_ListSize++;

			// Changing Actual data in Generation tool (monobehaviour)
			// not returning anything just updating the array size 
			m_generationScript.TreeSelectionUpdate(m_ListSize);

			m_TreeTypesSP.serializedObject.Update();
			var item = m_TreeTypesSP.GetArrayElementAtIndex(m_TreeTypesSP.arraySize - 1);

			var soItem = new SerializedObject(item.objectReferenceValue);
			var name = soItem.FindProperty("name");

Each list field in my inspector binds to the same value:

Unity_qoydkO8FJA

I’m unsure why this is because when the “item” is set it checks the index in the array. Surely that should set individual binding path.

Things I’ve tried:

anyproperty.serializedObject.applyModifiedProperties();
anyproperty.serializedObject.Update();

I would greatly appreciate if anyone could help me with this.
Thanks.

2 Answers

2

This is probably the issue here. How do you actually change the data? Directly on the actual object? In that case whatever you do is not reflected in the SerializedObject / SerializedProperty system. The SerializedObject does work on a copy of the serialized data which it pulls when you call Update on it or when it’s created.

Likewise directly manipulating the underlying object does not necessarily result in a change of the serialized data. Data manipulations have to be communicated to Unity. Unity may detect a change later or when a save happens at a later time but that’s generally not how it should be handled.

Note that a single SerializedObject can actually represent several objects at once (multi-object editing). So directly messing around with the underlying object AND using SerializedObject / Property at the same time doesn’t play well together.

When you do direct object manipulation, you usually want to use the Undo class and call RecordObject BEFORE you do the change. Though the changes aren’t applied until the current method ends. Also as I said, the data in the SerializedObject does not magically update itself. Keep in mind that calling Update on the SerializedObject will cause any changes that has been done to the SerializedObject / SerializedProperties that hasn’t yet been applied to be lost. That’s why it’s generally not recommended to mix those things.

We don’t really have much context of how your editor script looks like based on the provided snippets.

Hi Bunny83,

Thanks for your reply:

Here’s a snippet of my monobehaviour script that shows what TreeSelectionUpdate() does. I think you’re right thought about it changing the object directly:

[Header("Tree Type Variables")]
	[Space]
	public List<TreeType> m_TreeTypes;
	[SerializeField] private TreeType m_EmptyTreeType;

	public void OnEnable()
	{
		m_EmptyTreeType = ScriptableObject.CreateInstance<TreeType>();
	}


	public void TreeSelectionUpdate(int listSize)
	{
		m_TreeTypes = new List<TreeType>();

		for (int i = 0; i < listSize; i++)
		{
			m_TreeTypes.Add(m_EmptyTreeType);
		}
		Debug.Log(m_TreeTypes.Count);
	}


public class TreeType : ScriptableObject
{
	public string name;

	public GameObject prefab;

	public float spacing;

	public float frequency;
}

In the onClick functionality in the editor class, after i update the list, I copy what I did in the first code example i posted, adding a text field. But this time I change the BindingPath
to
BindProperty()

and passing in the value of the serialized property “name”:

			// Changing Actual data in Generation tool
			// not returning anything just updating the array size 
			m_generationScript.TreeSelectionUpdate(m_ListSize);

			m_TreeTypesSP.serializedObject.Update();
			var item = m_TreeTypesSP.GetArrayElementAtIndex(m_TreeTypesSP.arraySize - 1);

			var soItem = new SerializedObject(item.objectReferenceValue);
			var name = soItem.FindProperty("name");

			var Treename = new TextField();
			Treename.label = "Tree Name:";
			Treename.BindProperty(name);
			Treename.multiline = false;
			Treename.maxLength = 140;

Does this apply for my m_TreeTypes SerializedProperty only in my original examples? Since its the only one when your saving an object that has been directly modified. Can you show a basic example of this?

Would using the Undo class and calling record object on m_TreeTypesSP for example negate the use of calling update on it since then i would just be removing the changes I’ve just made?

Thanks again