Search Unity

Detect mouse click on object in editor mode (OnSceneGUI())

Discussion in '2D' started by Roryyyyyyyyyy, Jul 27, 2017.

  1. Roryyyyyyyyyy

    Roryyyyyyyyyy

    Joined:
    Jun 8, 2015
    Posts:
    22
    Hi, I am trying to set up a level editor type thing where when you click an object in a grid it changes. It is all in 2d but when I try to raycast2d it does not pick up any collision. The objects have circlecollider2ds and below is how I am raycasting:

    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Raycast(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin, new Vector2(0, 0));
    The origin is correct - I have checked it against the positions of the objects, but for some reason it isn't registering any hits.

    Any advice or links would be greatly appreciated.
     
  2. Roryyyyyyyyyy

    Roryyyyyyyyyy

    Joined:
    Jun 8, 2015
    Posts:
    22
    Just an update, I started Unity today, added a rigidbody2d to the object being clicked, it worked, removed it, it still worked. Don't know what changed overnight but it's fine now