Hi, I’ve searched for it everywhere but still no actual solution for JavaScript.
I have a prefab, and when I try to AddListener to it, it doesn’t work.
However I made this:
UIButton.onClick.AddListener(function() { Selection();});
Hi, I’ve searched for it everywhere but still no actual solution for JavaScript.
I have a prefab, and when I try to AddListener to it, it doesn’t work.
However I made this:
UIButton.onClick.AddListener(function() { Selection();});
This would be useful to the whole community as many still prefer JavaScript over C#
We need this. Anyone?
The argument is supposed to be a delegate. I don’t know how to create a delegate in UnityScript, but I expect you should be able to find the solution online.
I have the same problem.
Hi everyone,
I found this http://answers.unity3d.com/questions/905599/assigning-an-onclick-function-at-runtime.html
I add a script to my prefab and on Start function I add listener to onlick like this
(this is the script on my prefab)
function Start()
{
var myButton = this.GetComponent.<UI.Button>();
myButton.onClick.AddListener(function() { OnClicAnswer(); });
}
function OnClicAnswer()
{
Debug.Log("OnClickAnswer ");
}
it works !!!
Don’t know why you can’t addlistener outside this solution
Not sure if I understand what you mean with that.
As you want to react to a certain event that happens for a specific button, that behavior can be configured right there where it belongs. And there are other event to which you can react, not just onClick, that’s why you need to call myButton.onClick.AddListener.
Hi,
what I means is that i’ve tried to addListener on the same script where I instantiated my prefab.
Well, my bad, I retested and find that it works when I instantiated my prefab, I added the listener on the wrong object.