Editor event and selection problems?

Hello,
I’m making an editor script for a custom editor that works through the scene view. I have a few object that I made non-selectable and non-editable in the scene view with HideFlags. I have a parent of these objects that I still want to be selectable by clicking on the object (which is only made visible by children meshes). (So that editing of the children goes through the parent) So I made an OnSceneGUI function on the parent, and cast a ray to see if it hits a child. I want to make it select the parent if this succeeds. I know that it is succeeding with the ray cast because I log it, but it seems to be deselected immediately. I think either Event.Current.Use() isn’t working or that it’s still deselecting the objects even if the event is used. Here’s the code:

    public void OnSceneGUI()
    {
        if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseUp)
        {
            if (Event.current.button == 0)
            {
                Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                RaycastHit hitInfo;

                if (Physics.Raycast(worldRay, out hitInfo))
                {
                    if (hitInfo.transform.name.StartsWith("Terrain Section"))
                    {
                        Object[] transforms = new Transform[1];
                        transforms[0] = (Object)hitInfo.transform.parent;
                        Selection.objects = transforms;
                    }
                }
            }
        }
    }

Thanks.

Are you perhaps running into the same problem that’s described here? (The same issue is also discussed in the last post of this thread.)

I tried changing my code to do what they do in those links you sent, with the DefaultControl and such, and this is my new code. I get the same results.

public void OnSceneGUI()
    {
        int controlID = GUIUtility.GetControlID(CTerrainInspectorHash, FocusType.Passive);
        if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseUp || Event.current.type == EventType.MouseDrag)
        {
            if (Event.current.button == 0)
            {
                Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                RaycastHit hitInfo;

                if (Physics.Raycast(worldRay, out hitInfo))
                {
                    if (hitInfo.transform.name.StartsWith("Terrain Section"))
                    {
                        int[] transforms = new int[1];
                        transforms[0] = hitInfo.transform.parent.GetInstanceID();
                        Selection.instanceIDs = transforms;
                        Debug.Log("Hit");
                        Event.current.Use();
                    }
                }
            }
        }
        
        if (Event.current.type == EventType.Layout)
        {
            HandleUtility.AddDefaultControl(controlID);
        }

        if (GUI.changed)
            EditorUtility.SetDirty(target); 
    }

I couldn’t find any of those links assigning the hash, so I figured it was this.GetHashCode().

Ah, but then I don’t need my selection code. And that fixes the problem. Thanks!