- Home /
Serialized properties affecting multiple variables
I am hoping someone can help me since I am finding some strange but consistent behaviour with one of my editor scripts whilst selecting multiple objects. Basically if I edit one value it will change it And copy another value, that wasn't touched, into every other selected object so their values become identical (This is on the script itself, the gameobject is unaffected).
For example when I edit the delay value of a group of objects it will also copy the transition time of the last selected object as well even though it wasn't edited.
I have a feeling I am just doing something wrong with my editor script and the serialised properties, I have posted a redaction of it below:
void OnEnable ()
{
delay = serializedObject.FindProperty ("delay");
transitionTime = serializedObject.FindProperty("transitionTime");
}
void OnInspectorGUI()
{
serializedObject.Update ();
transitionTime.floatValue = EditorGUILayout.FloatField(new GUIContent("Transition Time", ""), transitionTime.floatValue);
delay.floatValue = EditorGUILayout.FloatField(new GUIContent("Delay", ""), delay.floatValue);
serializedObject.ApplyModifiedProperties ();
}
Cheers! Jason
Answer by Jamora · Nov 17, 2013 at 11:28 AM
The reason brain56 suggests for this behavior is correct. In order to be able to edit multiple objects, you will need the CanEditMultipleObjects -attribute on your class (which I assume you already have). Then, instead of
transitionTime.floatValue = EditorGUILayout.FloatField(new GUIContent("Transition Time", ""), transitionTime.floatValue);
delay.floatValue = EditorGUILayout.FloatField(new GUIContent("Delay", ""), delay.floatValue);
Use the PropertyField. Not only is it simpler - in that it will automagically detect which type the SerializedProperty is - but also achieves the functionality you're looking for.
EditorGUILayout.PropertyField(transitionTime);
EditorGUILayout.PropertyField(delay);
Answer by brain56 · Nov 16, 2013 at 07:47 PM
Your problem is this line:
serializedObject.ApplyModifiedProperties ();
With the lines:
transitionTime.floatValue = EditorGUILayout.FloatField(new GUIContent("Transition Time", ""), transitionTime.floatValue);
delay.floatValue = EditorGUILayout.FloatField(new GUIContent("Delay", ""), delay.floatValue);
You assign to transistionTime
and delay
the value values from the last selected object. What basically happens then, is you're assigning these last recorded values to whatever objects you selected.
Say for example we have:
// Object A
objA.transitionTime = 2.0f;
objA.delay = 0.2f;
// Object B
objB.transitionTime = 1.0f;
objB.delay = 0.1f;
First, select Object B. This will make the values of the editor script variables transitionTime
equal to 1.0f
and the value of the variable delay
0.1f
.
And then, select Object A. This will make the values of the editor script variables transitionTime
equal to 2.0f
and the value of the variable delay
0.2f
.
When you then proceed to call serializedObject.ApplyModifiedProperties ();
, you'll proceed to update the selected objects with the latest values of the variables, which are 2.0f
and 0.2f
for both transitionTime
and delay
respectively.
Hey brain56 thanks for your input but that doesn't really solve my problem. If I remove that line then nothing will ever update.
However your example is exactly right, any ideas on how to get around the problem?
Your answer
Follow this Question
Related Questions
Count multiple objects! 3 Answers
Having multiple gameobjects from one script, changing sprites in game 0 Answers
Multi-gun Aiming System 1 Answer
Only getting one game object instead of multiple game objects in start of the program 0 Answers
How to keep objects disabled after reloading the scene? 2 Answers