- Home /
Adding a function to a UI 4.6 Button called on another game object in c#
Hello a very similar question to :
http://answers.unity3d.com/questions/777818/46-ui-calling-function-on-button-click-via-script.html
However when I try this, the method I need to call doesn't get called and isn't listed on the button at runtime.
I think perhaps this is because when I create the button using the GUI I have to specify the parent object with the script in it to call the button.
MyButton.onClick.AddListener(() => { MyFunction();})
MyFunction is attached to the parent gameobject so I tried:
MyButton.onClick.AddListener(() => { transform.parent.GetComponent<myscript>().MyFunction();})
But this didn't work either.
I also tried this:
UnityAction myaction = new UnityAction (transform.parent.gameObject, transform.parent.GetComponent<myscript>().myFunction);
MyButton.onClick.AddListener(myaction);
But that really didn't work!
Yes the button exists and has a Button script attached, I can use it to successfully set several of its properties.
Your answer
Follow this Question
Related Questions
UI works in editor, but not on mobile device 1 Answer
I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers
I need to move a sprite when an UI button is clicked 1 Answer
Offsetting RectTransform based on button state button 1 Answer
OnClick() function not working!!! 2 Answers