Adding Listener to Buttons

I’m not too familiar with adding listeners and to buttons, but based off what I’ve read, the following should work:

using UnityEngine.UI;
using System.Collections.Generic;

public class DirectorListener : MonoBehaviour {

    public static DirectorListener self; 
    public Dictionary<string, GameObject> guiObj = new Dictionary<string, GameObject>();  

	void Start ()
    {
        getAllButtons(); 
	}

    void getAllButtons()
    {
        GameObject[] temp = Resources.FindObjectsOfTypeAll<GameObject>();

        for (int i = 0; i < temp.Length; i++)
        {
            if (temp*.GetComponent<Button>() != null)*

{
addButtonListener(temp*);*
guiObj.Add(temp_.name, temp*);
}
}
}*_

void addButtonListener(GameObject button)
{
string temp = button.name;
button.GetComponent().onClick.AddListener(() => onButtonClick(temp));
print(“Added Button Listener”);
}

void onButtonClick(string message)
{
print(message);
}
The goal is to cycle through all buttons in my program, and add a listener to them that will send a message to this script containing the name of the button. I do this instead of manually linking up the buttons because I an instantiating the buttons from prefabs at run-time.
Currently, it says that a listener is being added, but I do not get a message when I click any button. Anyone know what I am doing wrong?

Whilst it may seem weird, are you sure there is an EventSystem in your scene? You say you’re instantiating button prefabs, but without an EventSystem/Canvas UGui will not detect buttons or any GUI interactions. Failing that, here’s a little simplification you can do with your searching is [find objects of a certain script type][1], also (the more important tip:) to get more useful debugging data is using Debug.Log and having it point to a GameObject in your scene hierarchy:

 void getAllButtons()
 {
     Button[] temp = FindObjectsOfType<Button>();
     Debug.Log("getAllButtons search, has found this many buttons: " + temp.Length);

     for (int i = 0; i < temp.Length; i++)
     {
             Debug.Log("getAllButtons looping, setting addListener for: " + temp_.gameObject.name, temp*.gameObject);*_

addButtonListener(temp*);*
guiObj.Add(temp_.name, temp*);
}
}*_

void addButtonListener(Button button)
{
string temp = button.name;
button.onClick.AddListener(() => onButtonClick(temp));
// this is twofold: Debug uses Unity’s logging, and also saves it to an output text file on builds,
// and the " , " makes the debug point to a gameobject in the scene hierarchy whilst the editor is running
Debug.Log(“Added Button Listener”, button.gameObject);
}
_*[1]: http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html*_