Show Text where user clicks in a panel,Show text where User clicks
Hi, my task is pretty simple: I want to show a text where the user clicks on a panel. Problem: The Text does not appear where I click. I tried with different Coordinate Transformation, anchoredPositions, etc. Nothing helps. I'm super frustrated now because even the simplest tasks seems to be a massive pain in Unity when you are new to it. Here is my code:
public void OnPointerClick(BaseEventData pointer)
{
PointerEventData pointerEventData = pointer as PointerEventData;
playerGold++;
clickDisplayTextbox.GetComponent<Text>().text = "+ 1";
RectTransform clickDisplayTextbox_1RT = clickDisplayTextbox.GetComponent<RectTransform>();
Debug.Log("Clicked:" + pointerEventData.pressPosition.x + " " + pointerEventData.pressPosition.y);
Vector3 transformedCoords = new Vector3(pointerEventData.pressPosition.x, pointerEventData.pressPosition.y, 0f);
clickDisplayTextbox_1RT.localPosition = transformedCoords;
Debug.Log("localPosition Position:" + clickDisplayTextbox_1RT.localPosition.x + " " + clickDisplayTextbox_1RT.localPosition.y);
}
A visualization of the problem can be seen here, the red dot is where I click. The "+1" is the text that appears.
However, the +1 appears somewhere completely else. Maybe some experienced dev can give me a hint what's wrong? Best regards, Simon
Ps: I am using a Canvas with "Scale with Screen size" Setting, so it matches the camera.
,Hi, my task is pretty simple: I want to show a text where the user clicks on a panel. Problem: The Text does not appear where I click. I tried with different Coordinate Transformation, anchoredPositions, etc. Nothing helps. I'm super frustrated now because even the simplest tasks seems to be a massive pain in Unity when you are new to it. Here is my code:
public void OnPointerClick(BaseEventData pointer)
{
PointerEventData pointerEventData = pointer as PointerEventData;
playerGold++;
clickDisplayTextbox.GetComponent<Text>().text = "+ 1";
RectTransform clickDisplayTextbox_1RT = clickDisplayTextbox.GetComponent<RectTransform>();
Debug.Log("Clicked:" + pointerEventData.pressPosition.x + " " + pointerEventData.pressPosition.y);
Vector3 transformedCoords = new Vector3(pointerEventData.pressPosition.x, pointerEventData.pressPosition.y, 0f);
clickDisplayTextbox_1RT.localPosition = transformedCoords;
Debug.Log("localPosition Position:" + clickDisplayTextbox_1RT.localPosition.x + " " + clickDisplayTextbox_1RT.localPosition.y);
}
A visualization of the problem can be seen here, the red dot is where I click.
However, the +1 appears somewhere completely else. Maybe some experienced dev can give me a hint what's wrong? Best regards, Simon Ps: I am using a Canvas with "Scale with Screen size" Setting, so it matches the camera.
Your answer
Follow this Question
Related Questions
Having text be a child to a game object? 2 Answers
Text Objects do Lerp 0 Answers
Search for Object with largest y-Coordinates 1 Answer
Render to minimap on command 1 Answer
How to tell if a looked at object has a text mesh. 0 Answers