Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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:
    21
    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:
    21
    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