Search Unity

How to use OnClick() with two functions in same script?

Discussion in 'UGUI & TextMesh Pro' started by caseyboundless, Aug 25, 2014.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I have MoveRight() wired to my button when clicked my player moves right. I duplicated my button and changed it to left button. I go to change the OnClick() (Basket) to MoveLeft() but nothing shows up?
     

    Attached Files:

  2. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    The function is public right?
     
  3. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Code (CSharp):
    1. public void MoveRight()
    2.     {
    3.        
    4.         moveRight = true;
    5.        
    6.     }
    7.  
    8.     public void MoveLeft()
    9.     {
    10.        
    11.         moveLeft = true;
    12.        
    13.     }
     
  4. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    In situations like these, first thing I do is I simply cut and paste the functions so the bottom one is now on top. Then I would see if the existing, working link is still configured and then check if the other function now appears when I use the other button.

    From your post it sounds like you are under the impression that you can only have one function visible per script. This is a good way to verify if you are correct or not.

    First time I tried to attach functions to actions I couldn't get any of my functions to show up. Finally I figured out I need to make delegates of type System.Action and call the delegates before they show up... then a while later I realised I was a complete idiot... My public functions appeared in the list but it was hidden in between the other available functions and all I had to do was open my eyes :p

    Per haps you are having a suitably blond moment as well...? So simple enough thing to try: replace the order of those two functions in the script and see if anything changes :)