Search Unity

Need Help with determining what object is selected

Discussion in 'Editor & General Support' started by Maker16, Sep 4, 2011.

  1. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    I'm working on an editor script. I need to get a reference to an object in the scene view when the user clicks on it. I have tried using Selection,activeObject to test for the object in question, but that doesn't work on children. If a child is clicked, it only returns the parent.

    SO, I'm trying the hacky way of raycasting using the following code:
    Code (csharp):
    1. function OnSceneGUI()
    2.     {
    3.         if(Event.current.type == EventType.mouseDown)
    4.         {
    5.             var obj : GameObject;
    6.             var ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
    7.             var hit : RaycastHit;
    8.             if (Physics.Raycast (ray, hit, Mathf.Infinity))
    9.             {  
    10.                 Debug.Log(hit.collider);
    11.             }
    12.         }
    13.     }
    This doesn't work because it is using the main camera, and that isn't the camera used in the scene view. I can't find much documentation on working with the scene view. Is there a scene view camera I can use to make that raycast work, or is there some way to use the Selection class to pick out a child object that is selected?