# OnClick unity from another class

I have class "Button" with function

public void SetOnClickListener(UnityAction listener)
{
}


I have class "A" with fllow code

void D()
{
Button btn = new Button();
btn.SetOnClickListener(new UnityAction(SomeFunction));
}
public void SomeFunction()
{
Debug.Log("Some Function was called!");
}


And it doesn't work. But if I do next change->

void D()
{
Button btn = new Button();
btn.SetOnClickListener(new UnityAction(SomeFunction));
}
public static void SomeFunction()
{
Debug.Log("Some Function was called!");
}


All work correctly. Why I must use static and can I not use it?

• I am afraid question asking the meaning of static keyword is about general programming. Official documentation of static or this question might be helpful and/or some basic programming tutorials. – wondra Jun 21 '17 at 12:16
• you should read this topic, since it is basically the same stackoverflow.com/questions/10795502/… – SuperLog GK Jun 21 '17 at 12:20
• @wondra: while asking about the static keyword in general would be general programming, I think this question's specific focus on "why does a UnityAction require the method be static in this situation" makes this worth answering here on the gamedev exchange. We can construct event systems that don't need static this way, so it's really a question about how Unity's system works, not about static in general. – DMGregory Jun 21 '17 at 12:25
• @DMGregory if OP knew what static was, he would understand what is wrong and how to fix it. Seeing the answer (covering the general programming aspect), it is less detailed version of those in linked SO question, and I am not sure if that is helpful for future references... but I guess having duplicate questions between sites does not hurt neither. – wondra Jun 21 '17 at 12:28

You can try passing button.onClick.AddListener(delegate { this.SomeFunction(); });instead of new UnityAction(...). - This should work and that is how I do it.
Also, I guess you have your own class Button because of the constructor in UnityEngine.UI.Button is protected and cannot be instantiated.