Search Unity

getting transform of ui button

Discussion in 'Getting Started' started by witcher101, Nov 1, 2015.

  1. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    I have a button in ui. In the onclick componeent i have object with script attached to it.
    I am trying to get the transform of clicked ui button.
    But when i use print(this.name) it gives name of name of the object in which cript is attached, not the name of ui button which i clicked.
    How do i get the clicked ui button to change its transform
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You just have to move the script onto the button. Once you've passed the event off to an event handler on another object, there is no way to know where it came from.
     
  3. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    But ui buttons dont accept scripts. It only accept gameobject with script
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What? UI buttons are game objects like any other. You can attach scripts to any game object, whether it's a visible 3D object, a visible UI object, or a completely empty (and thus invisible) GameObject. It makes no difference whatsoever.
     
  5. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    If i add script as a component, then when if i click one button, it acts as if everybutton was clicked.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Then you haven't hooked it up correctly, but without more detail, I can't imagine exactly how. Consider posting a more detailed question, with screen shots.
     
  7. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    I have 10 ui buttons. On each buuton i have script attached
    public int price;


    void Update () {
    if (Input.GetMouseButtonDown(0))
    {
    print(price);
    }

    So when i click on 1 button in console 10 print commands are shown
     
  8. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    That's because you're not doing it right. :)

    Input.GetMouseButtonDown(0) is saying "whenever the mouse is clicked, do this". What you actually want is to get the Button component of the object, and add a listener for its OnClick event.

    Code (CSharp):
    1. Button b = gameObject.GetComponent<Button>();
    2. b.onClick.AddListener(delegate() {
    3.   // Do stuff here
    4. });
    There are other ways to do this as well, including Unity's built in Event system.
     
    JoeStrout likes this.
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Right. UI elements like Button already do things like properly track mouse clicks, and figure out what's a valid click and what is not, and which button it is intended for. So, once you have buttons on a canvas, you should no longer be thinking about mice or clicks at all — you should be thinking about events, like the On Click handler.

    I don't actually recommend beginners attempt to hook up events in code though, as @Schneider21 illustrated above. It can work, but then you have to remember to remove the listener when the object is deactivated, etc. It can be a little tricky to get right if you don't understand how it all works pretty deeply.

    However, hooking them up in the inspector is quite easy. Start here and spend an hour or two going through tutorials, and you'll have it down!
     
    Schneider21 likes this.
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664