Imagine I have many buttons on the screen and the player clicks on a specific button. How do I save the text of that button the player pressed?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class ClickedAnswer : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public static string PlayerAnswer;
public void OnPointerDown(PointerEventData eventData)
{
PlayerAnswer = //What do I type here?
}
}
That’s what I need to do, to define which button the player clicked.
I tried something new. I typed “public Text PlayerText” and I added the script in the inspector to a button, however I still don’t have the option of dragging a text object in the hierarchy to it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class ClickedAnswer : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public static string PlayerAnswer;
public Text PlayerText;
public void OnPointerDown(PointerEventData eventData)
{
PlayerAnswer = PlayerText;
}
}
Well you cant just “i clicked so decide which i clicked”. you need to define buttons, you could set in buttons that when it is pressed it sets in clickedanswer some int variable to lets say 1.
then check that int variable and if it is set to 1 answer is “Yes, i would like some icream” if set to 2 “I like pie more” and so on.
maybe each button activates function in clicked answer and sets int (that would be instant answer).
eventData.pointerPress will give you the GameObject that triggered the event. I don’t know exactly how you have this set up, but if it works like a default button, you’d use:
I have never personally had a reason to click on text, but I’m sure you can just make a button, set the material of the button to nothing, and just parent text to it.