InspectorEditor: Find target object that has focus


I’m working on an InspectorEditor for my asset.
When multiple objects are selected with varying data, they need to be synced before the user can edit the data.
For that, I check all the serializedObject.targetObjects if they have similar data.
When the data is not synced, I want the data of the focused object to be used for all other selected objects.

I found, that the order of the objects in the targetObjects array does not reflect the order at which they have been clicked. So the focused element can be anywhere in that array.

Is there a way how I can determine which of those objects is the focused object?

For clarity:
If you select multiple objects in the scene view, there is one object that has a light blue mesh and (when pivot mode is selected) contains the tool handle. I call that one the focused object.

Thank you

To answer my own question:

The serializedObject.targetObjectsdoes not reflect the selection order.
But the Selection.activeObject does.

So by comparing the serializedObject.targetObjects with the Selection.activeObject you can find the currently focused object.
Usually the gameObject containing the component is selected, but it is also possible to select a component directly through code.
So it’s important to compare the gameObject AND the component with the Selection.activeObject.

I’ve writen this method to find the index of the focused serializedObject.targetObjects:

public int FocusedTargetIndex()
    var selected = Selection.activeObject;
    for (var i = 0; i < serializedObject.targetObjects.Length; i++)
        var current = (MonoBehaviour)serializedObject.targetObjects*;*

if(current.gameObject == selected || current == selected)
return i;

return -1;
This code has worked for me.
If someone finds an error/bug/whatever, please let me know :slight_smile: .