Is there a way to detect if a button is pressed if it isn't created programatically?

I notice that every example for detecting if a button is pressed looks like this:

void OnGUI(){
      if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
                Debug.Log("Clicked the button with an image");
}

Which is great if you created the button programatically. But what if you created the button using the Create menu? Then how would you access it in an if statement inside the OnGUI function like we see here, but without the programmatic creation?

Firstly, using OnGUI() is legacy code and not recommended anymore (although you are free to do so).
Instead Unity recommends using it’s UI elements. (You can find them in Create → UI)

A very easy way to do what you want is with UI Buttons:

  •  Video tutorial: https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button  
    
  •  Manual: https://docs.unity3d.com/Manual/script-Button.html  
    
  •  Scripting Reference: https://docs.unity3d.com/ScriptReference/UI.Button.html
    

If you want to add an event callback to a button via script you can do the following:

GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => CallThisFunctionOnButtonClick());

This code will call a function called CallThisFunctionOnButtonClick() when the button element attached to the gameobject is clicked.
Note: This is strictly for use with the Unity UI.

Cheers!

Adding to this answer as I was trying to find a way to detect which button was pressed to toggle the text within side the button when done so without having to create handlers for each button. This works for me.

public void OnClick(Button b)
{
    if(b.name == "Button2D")
    {
        if (mainCamera.orthographic == false)
        {
            mainCamera.orthographic = true;
            b.gameObject.GetComponentInChildren<Text>().text = "3D View";
        }
        else
        {
            mainCamera.orthographic = false;
            b.gameObject.GetComponentInChildren<Text>().text = "2D View";
        }
    }

}