Editor script - apply modified fields outside target class

Hi there,

Summary
I want to create kind of an editor tool where I assign a field value once and it automatically assigns them to specified components (different classes than the class where I assign the value), so I do not have to go into each and every component and manually assign them (which is prone to error and also time consuming).
I wrote a custom inspector for this that does indeed assign all values correctly. However, I cannot find a way to serializedObject.applyModifiedFields to objects that are not the target object. So the values on all objects except for the target object are reset when I hit Play.

This is the situation:
Let’s say I have GameObject 1 with a component class A attached to it. I wrote a custom inspector for it called A_Editor. A has a field called “name” (type string) that is assigned in the inspector.
GameObject 1 has a couple of children, let’s say GameObject 2 and GameObject 3.
GameObject 2 has a component class B attached with a field called “name” (type string), object 3 has a component class C attached with a field called “name” (type string), too.

All these classes (A, B, C) need the same value for the field “name”.

What I want to have:
Assign “name” in A’s inspector.
Hit a button in A’s inspector that automatically assigns the value of “name” to the “name” fields of classes B and C.

What I have so far
A has a method called “setup”. In this method it uses getComponent to create references to GameObjects 2 and 3 and their classes B and C and set their “name” value.

A_Editor has a button that calls A’s method “setup”. This works fine so far, it sets the correct values when I check the inspectors of object 2 and 3.

The problem
When I hit play, all values of the components B and C on GameObjects 2 and 3 are reset. I think this is because i did not apply the changes. I use serializedObject.applyModifiedFields on my target (class A) and A keeps its values, but I don’t know how to do this for B and C.

Does anybody have an idea how to achieve this?

If it doesn’t work at all, I can also call the setup on class A’s Awake - this does the job, too, but I want to avoid this bc. of performance and also bc it is prone to errors and confusion to have values displayed in the inspector that are not actually “real”.

I appreciate any help and suggestions for my problem. Thanks in advance! :slight_smile:

This won’t work. You should also use a SerializedObject to manipulate the fields of your other objects. Or if you really manipulate the objects directly, you have to use the Undo class to register the object(s) before the manipulation.

1 Like

Hi, thanks a lot, this was really helpful! I tested both approaches and I think I’ll stick to the Undo class - it’s exactly what I needed :slight_smile:

Hi, I am running into some issues. I am using Undo.RecordObject in my editor script to make sure the changes done to the classes B and C are applied. However, it keeps resetting the values on Play if Object 1 is not selected (object 1 has a component class A for which I wrote the custom inspector).

I made following observations:
case 1:
1: I select Object 1 and change my fields.
2: I select Object 2 or 3 to check - all fields are assigned correctly
3: I select Object 1 and hit Play
4: I select Object 2 or 3: all fields are assigned correctly
5: I select Object 1 and stop Playing
6: I select object 2 or 3: all fields are still assigned correctly

case 2:
1: I select Object 1 and change my fields.
2: I select Object 2 or 3 to check - all fields are assigned correctly
3: I hit Play → the fields reset

case 3:
1: I select Object 1 and change my fields.
2: I select Object 2 or 3 to check - all fields are assigned correctly
3: I select Object 1 and hit Play
4: I select Object 2 or 3: all fields are still assigned correctly
5: I keep Object 2 or 3 selected and stop Playing → the fields reset

I am (as you might have noticed :wink: ) quite new to editor scripting and I have no clue why this issue occurs.
Do you have any idea what causes this and how I can solve it?

Ah, nevermind, I found out what caused the issue, so here it is, if anyone has the same problem:
The modified objects are part of a prefab so I had to use PrefabUtility.RecordPrefabInstancePropertyModifications(my classes) to actually apply the changes.
Now I hope they also persist when I close the scene or Unity and open it again.