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. Dismiss Notice

Question How do I make an empty region hittable by EventSystem.RaycastAll?

Discussion in 'UGUI & TextMesh Pro' started by mortoray, Aug 11, 2023.

  1. mortoray

    mortoray

    Joined:
    Sep 14, 2018
    Posts:
    89
    I have an GameObject that doesn't have a graphic itself, but is the parent to several children, using a grid layout for example. I want this "empty" (no graphic) parent, to be a target for EventSystem.RaycastAll.

    I have tried two approaches:
    - Adding a BoxCollider2D, but apparently EventSystem.RaycastAll does not consider these colliders
    - Deriving from Graphic, this makes it a target, but also expects a graphic, drawing a white box by default

    What is a good approach to getting an empty region to be a target?

    Note, I don't want to handle clicks on this object directly. I have a help pointer: the user clicks help, then an object to get help. I'm using RaycastAll to find targets. I actually only want to find one target, but I can't filter layers with this function. Is there perhaps another raycaster that works with UI elements and all colliders at once?
     
  2. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    755
    correct

    would setting the alpha to 0 be an issue? i.e. yourGraphicComponent.color = Color.clear

    it's not clear why you don't want a graphic component on that parent, so I cannot think of a "broader" solution.
    at least visually, having no Graphic and having a Graphic with zero alpha are the same thing.

    EventSystem.RaycastAll should return all results, so you can identify everything in the returned list (for ex. you know the objects' names or tags)