1
\$\begingroup\$

I have class "Button" with function

public void SetOnClickListener(UnityAction listener)
{
    btn.onClick.AddListener(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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – wondra Jun 21 '17 at 12:16
  • 1
    \$\begingroup\$ you should read this topic, since it is basically the same stackoverflow.com/questions/10795502/… \$\endgroup\$ – SuperLog GK Jun 21 '17 at 12:20
  • \$\begingroup\$ @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. \$\endgroup\$ – DMGregory Jun 21 '17 at 12:25
  • \$\begingroup\$ @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. \$\endgroup\$ – wondra Jun 21 '17 at 12:28
1
\$\begingroup\$

Because with a static method all it needs is function index to store a reference to it. But when you are trying to pass member function it doesn't have a reference to an instance to call it from.

If you want to learn more about function pointers the best way I know of is to try implementing delegate or action in C++.

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.

\$\endgroup\$
  • \$\begingroup\$ It also helps if the button object lives longer than the method it is created in. When the function in the question returns, that Button object is garbage collected and the listener attached to it goes with it, the same as if it never existed. \$\endgroup\$ – Draco18s Jun 21 '17 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.