How can I add function to onClick event by code?

I’ve got problem with adding function to a button. I’m reloading scene many times on runtime and button loses its function. I can’t add it from Inspector so i need to do it by code. I’ve tried this

void OnLevelWasLoaded(int level) {
        if (level == 0) {
            GameObject button = GameObject.Find("Sound");
            Button b = button.GetComponent<Button>();
//            b.onClick.AddListener(() => AudioSwitch());
            b.onClick.AddListener(delegate { AudioSwitch(); });
        }
    }

but it does not work for me. How can I do that?

57591-screen.png

Try this:

b.onClick.AddListener(AudioSwitch);

your problem is including the paren’s after AudioSwitch.
AudioSwitch means “use the function AudioSwitch itself here”.
AudioSwitch() means “call AudioSwitch, and use whatever it returns here”.

so assuming AudioSwitch() was a void function,
your code is equivalent to

b.onClick.AddListener(() => null);