TMP_TextUtilities.FindIntersectingXXX always returning -1

I´m trying to code a clickable link feature for TMP. After failing several times I found out that the FindIntersectingXXX Functions always return -1 for me. I tried an empty scene with a new canvas and TMPUGUI component but it still fails for me.

any Ideas on the Problem?

private void Update()
    {
        int wordIndex = TMP_TextUtilities.FindIntersectingWord(tmp_text, Input.mousePosition, Camera.main);
        int linkIndex = TMP_TextUtilities.FindIntersectingLink(tmp_text, Input.mousePosition, Camera.main);
        DebugPanel.Log("wordIndex", wordIndex);
        DebugPanel.Log("linkIndex", linkIndex);
    }

Unity Version: 2017.3.1f1

When using a Canvas which might be in screen space overlay, there is no camera per say. In this case, pass Null for the camera.

7 Likes

I pass null for the camera but the app does nothing when I click on a link. The code works when linkIndex != -1 but it’s like the script stops working when I do click on a link.

The variable linkIndex doesn’t even get a value when I click on the link. I set a breakpoint on the line and I can’t get any value, it’s as if the code just doesn’t execute anymore.

EDIT: nothing to do with the script, sorry. My link was at the bottom of a viewport and clicks aren’t being registered there. That’s another issue I have to figure it out now

It’s possible that the plane distance on the canvas camera or order in the layer is set wrong.

Try to change those values.

I just ran into this issue, fixed by passing null instead of Camera.main to TMP_TextUtilities.FindIntersectingLink

the reason:
if you passed in a camera, the position you passed in (pixel coordinates in screen space) will be converted to unit coordinates in world space (so you can find intersects for text links in world space).
if you’re using ScreenSpace Overlay canvas, pass camera as null so it would skip converting position from screen space to world space.