Search Unity

Making a selectable UI object

Discussion in 'Getting Started' started by IK, Mar 10, 2016.

  1. IK

    IK

    Joined:
    Oct 29, 2012
    Posts:
    34
    Greetings,

    I have normally used something like this to make a sprite clickable in my current project:
    Code (CSharp):
    1.                     clickableThing.AddComponent<scriptR>();
    2.                     clickableThing.GetComponent<scriptR>().setReference(r);
    3.                     clickableThing.AddComponent<BoxCollider2D>();
    *ScriptR contains a reference to thing and an onClick function.

    However this won't work in the UI. I've found I need to use a button and add a onClick listener I'm having trouble getting it to work right.

    If I add the following it still fails:

    Code (CSharp):
    1. clickableThing.AddComponent<Button>();
    2. clickableThing.GetComponent<Button>().onClick.AddListener(() => {clickableThing.GetComponent<scriptR>().copyOfOnClickFunction})
    What's the right way of doing this for things in the UI?
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    IK likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    IK likes this.
  4. IK

    IK

    Joined:
    Oct 29, 2012
    Posts:
    34
    IPointerClickHandler was exactly what I was looking for! Thanks!