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! ![]()