Author Topic: In new NGUI how to use Button to call a function in other script.  (Read 10351 times)

firoz989

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Hello Everyone, I want to know how can we call a function in other script when a NGUI button is clicked. I older version I do this by using "button message" (assigning a  gameobject and name the function which I want to run with the click of that particular button ) but now with "event trigger" how to do that please tell..
Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: In new NGUI how to use Button to call a function in other script.
« Reply #1 on: February 10, 2014, 05:05:07 PM »
Buttons have "On Click" notification you can set in inspector. If you're doing it via code, you can use EventDelegate.Add(button.onClick, YourFunction);

It's the same with the event trigger. It has "onClick" as one of its possible event triggers.

firoz989

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: In new NGUI how to use Button to call a function in other script.
« Reply #2 on: February 11, 2014, 12:41:18 PM »
thank you ArenMook for  replying but i am not good in programig so not able to understand what you said. Still I dont know how to run a particular function in an script which is attached to another gameobject when a particular buttons is clicked..

I have an empty gameobject to which i attached a script which have many functions, each function is for a particular task and there are different buttons for each task. Please tell in steps how to run a particular function when a particular button is clicked..Sorry for the trouble :-[  and thanks a lot for your help :).

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: In new NGUI how to use Button to call a function in other script.
« Reply #3 on: February 11, 2014, 01:20:16 PM »
At the bottom of your 3rd image, there's a button "Add Component".  Click this, and add a new EventTrigger component.  Your Inspector should now have something like this at the bottom:



If you click the little triangle to the left of "On Click/Tap", it gives you a way to add your remote function, as you used to do.  Just drag the GameObject that you want to execute the method onto where it says None, and then select the Method name that you want to call from the dropdown that appears below it.

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: In new NGUI how to use Button to call a function in other script.
« Reply #4 on: February 11, 2014, 01:25:56 PM »
Actually, it's even easier than I just said.  Since you only want to use OnClick, that's already in your UIButton component.  Just drag your game object with your scripts on it onto that spot in UIButton where it says None.  Then you'll get a drop down for what Method to call right there.

firoz989

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: In new NGUI how to use Button to call a function in other script.
« Reply #5 on: February 12, 2014, 04:41:15 AM »
Thanks Arcanor for your help but after assigning the gameobject(with a script) to the notify option, in drop down I am not getting the list of my functions or anything, How can i assign a particular function in the script to that button from drop down. I put the line "EventDelegate.Add(button.onClick, funshop); " in the start function of the script than I got errrors. So  please tell me what changes I have to made in my script so that I can get a list of functions in drop down. Thanks a lot for the help..

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: In new NGUI how to use Button to call a function in other script.
« Reply #6 on: February 12, 2014, 04:46:15 AM »
You don't need any script changes for this.  Get rid of the EventDelegateAdd().  Just make sure the function you're trying to call is public, and takes no parameters.  Then it should appear in the dropdown list of Methods.

firoz989

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: In new NGUI how to use Button to call a function in other script.
« Reply #7 on: February 12, 2014, 07:08:58 AM »
Thanks a lot Arcanor finally it worked for me....Thanks a ton bro... :)