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 IPointerXXXHandler's not firing for only some prefabs.

Discussion in 'UGUI & TextMesh Pro' started by rosco_y, Jul 10, 2023.

  1. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    I have a problem with the IpointerClickedHandler Interface. For a long time, it worked perfectly for me, but now, it no longer responds in any way to the cube prefabs outside of the region marked in red (see graphic below). My prefabs are relatively simple, I feel you’d agree.



    It is a cube with rounded corners (imported from Blender) with a single script. I’ve cut out the parts of the script which I don’t believe are significant to this problem.

    Code (CSharp):
    1.  public class SudoCube : MonoBehaviour, IPointerDownHandler,
    2.         IPointerUpHandler,
    3.         IPointerClickHandler,
    4.         IPointerEnterHandler,
    5.         IpointerExitHandler
    6. {
    7.   public void OnPointerClick(PointerEventData eventData)
    8.     {
    9.         print($"{ID}: OnPointerClick");
    10.     }
    11.         public void OnPointerDown(PointerEventData eventData)
    12.     {
    13.         print($"{ID}: OnPointerDown");
    14.     }
    15.  
    16.     public void OnPointerUp(PointerEventData eventData)
    17.     {
    18.         print($"{ID}: OnPointerUp");
    19.     }
    20.  
    21.     public void OnPointerEnter(PointerEventData eventData)
    22.     {
    23.         print($"{ID}: OnPointerEnter");
    24.     }
    25.  
    26.     public void OnPointerExit(PointerEventData eventData)
    27.     {
    28.         print($"{ID}: OnPointerExit");
    29.     }
    30.  
    31.     public int ID
    32.     {
    33.         get { return _hashCode; }
    34.     }
    35. }
    36.  
    To be thorough, I added all of the IpointerXXXXHandler interfaces that I found, to see if any of them would fire—if one did, I thought I could possibly make use of that ( cut out code which I feel is irrelevant to the problem):

    sudocube with red marked region.png

    Thank you for any help you can give me!

    Ross Ylitalo
     

    Attached Files:

  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
    There's about 10 possible things that might cause this. I am now assuming what you said, that your red zone area works but the top area doesn't work. Then things to check:
    • Is your red zone on the same canvas?
    • Check in scene view: there is no invisble RectTransform obstructing your top area
    • Your gameobjects have the SudoCube component, are active and the component is enabled?
    • Your gameobjects that dont work, they have colliders? If not colliders but you're targeting Images or say TextMeshProUGUIs, they have "Raycast Target" checked?
     
    Last edited: Jul 10, 2023
    rosco_y likes this.
  3. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    Thank you very much for your quick reply!

    Yes, you are right in that it has something to do with a canvas. It seems to have something to do with the way I've set up my GUI using the new UI Toolkit. I have a couple of labels and 12 buttons laid out on a Canvas (Screen Space - Overlay). When I disable the Canvas, all of my prefabs are once again Clickable, but unfortunately, this disables my GUI.

    The canvas for my UIDocument Covers all my prefabs, so it's a bit odd that I can click on the prefabs in the red zone, but nowhere else.

    I'm new to the UI Toolkit; the tutorial I followed started by suggesting I add a Canvas to my scene. I wonder if I'd be better off starting by opening the Toolkit and using that to set up my GUI, (instead of adding canvas, and then adding a UIDocument component to that.)?

    I haven't gone through all of the items in the checklist you gave me yet, as the first item in the list seems to offer promise., but I'll take a closer look at them now.

    Thank you again!

    Ross Ylitalo
     
    Last edited: Jul 10, 2023
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
    Okay, I am not familiar with the UI toolkit and can't help you from experience. I hope someone here can help you!
     
  5. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    You have helped much more than you know...now I have something to work on--I will try to redo my interface using slightly different methods--I feel it'll be worth the effort.

    I'll try to remember to let you know if that works, and all the best to you!

    Thank you very, very much,
     
    KillDashNine likes this.
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    UI Toolkit has no canvases, so if you've set up an old uGUI canvas alongside a UIDocument component, that's unnecessary, and possibly blocking inputs.

    You will also need to set some visual elements picking mode to
    PickingMode.Ignore
    , perhaps all of them, so you can click through them.
     
    Last edited: Jul 11, 2023
  7. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    I said I'd try to reply when I figured out the problem. Well, it doesn't seem to be the canvas after all, but it involves something called a "Visual Tree Asset" in a "UIDocument." As soon as I remove or disable the required "Source Asset" in my UIDocument, the problem disappears completely.

    I think I'll have to go back to the old way of building my UI. I'm such a newbie that I can't remember what the old UI system is called. I just know that I thought it was very non-intuitive. Oh well, I just hope that works.

    Thanks again,

    Ross
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
  9. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
    Regarding Unity UI (not toolkit) -

    When the player points at gameobjects with mouse, this is best done with raycasting from the camera to the gameobject. Now, if you put a canvas in between the camera and the gameobjects, you need to consider what you want to do. Do you want your raycast to hit the element on the canvas, or do you want the ray to pass through it? This is selected with the "Raycast Target" element, colliders and blocking masks, on the UI elements. Your IPointerXXXHandlers are listening to message events that come from the raycaster hitting those UI elements.

    Another thing, don't link your gameobjects to your UI elements with references or in any other way. Put your game scripts and UI scripts in a separate namespace and don't import your UI namespace from your game scripts. Instead, use events or messaging.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    So you know, this is nothing to do with 2D. The 2D forum is not for UI stuff.

    I'm going to move this post to one of the UI forums.