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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Button: retrieve information about clicked button

Discussion in 'Input System' started by kapkbmw, Oct 21, 2022.

  1. kapkbmw

    kapkbmw

    Joined:
    Oct 18, 2022
    Posts:
    2
    Hello,
    I am developing a survey in Unity that reads a text-file with information about e.g. a question, number of response buttons and labels of response buttons.
    These buttons are then displayed on a canvas with the corresponding labels.
    (e.g. not at all, a bit, completely)
    x x x

    I'm now stuck with sampling which button was clicked..the buttons are instanciated dynamically, so all buttons have the same onClick.AddListener... Is there a simple solution where i can just access the clicked button e.g. via an onClick-function to access and save the text-component (e.g. if the button with completely is clicked, then "completely" is saved)

    Thanks for your help
     
  2. kapkbmw

    kapkbmw

    Joined:
    Oct 18, 2022
    Posts:
    2
    i was blind yesterday...found it pretty quickly today

    using UnityEngine.EventSystems;

    and this in the listener function:
    string selected = EventSystem.current.currentSelectedGameObject.transform.Find("Text").GetComponent<TMP_Text>().text;
    solved the problem