Tasharen Entertainment Forum
Support => NGUI 3 Support => Topic started by: firoz989 on February 10, 2014, 06:37:37 AM
-
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.
-
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.
-
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 :).
-
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:
(http://content.screencast.com/users/waltenstein/folders/Snagit/media/cba25ff8-3827-4a87-810a-5f2e4845be6d/02.11.2014-13.18.png)
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.
-
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.
-
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..
-
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.
-
Thanks a lot Arcanor finally it worked for me....Thanks a ton bro... :)