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.
public class SudoCube : MonoBehaviour, IPointerDownHandler,
IPointerUpHandler,
IPointerClickHandler,
IPointerEnterHandler,
IpointerExitHandler
{
public void OnPointerClick(PointerEventData eventData)
{
print($"{ID}: OnPointerClick");
}
public void OnPointerDown(PointerEventData eventData)
{
print($"{ID}: OnPointerDown");
}
public void OnPointerUp(PointerEventData eventData)
{
print($"{ID}: OnPointerUp");
}
public void OnPointerEnter(PointerEventData eventData)
{
print($"{ID}: OnPointerEnter");
}
public void OnPointerExit(PointerEventData eventData)
{
print($"{ID}: OnPointerExit");
}
public int ID
{
get { return _hashCode; }
}
}
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):
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?
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.
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!
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.
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.
You asked (in another post) if I’ve tried your suggestions…I’m not using uGUI, and I don’t understand your suggestion to set PickingMode.Ignore, so I didn’t act on that. It is interesting though because I’m finding that my original problem of clicks not being detected in the “red” region has disappeared (see #1 ). Now I’ve got another problem though, I have to click twice on my cubes before my IPointerUpHandler handler event fires: I put a breakpoint on the first line in the handler, and I only hit it after the second time I click the object with the handler.
I know this post is getting old, but do you know what might be causing this double-click problem?