Using GUI and check what button was pressed

I tried using a premade button from Unity and refer to a function in a script of a different gameobject (using the Unity tutorial for GUI)

I gave each button it’s own specific name. But when I click the button it tells me: Object reference not set to an instance of an object.

Here is the script. the button refers to function BuildChoice:

public class BuildingManager : MonoBehaviour {
public GameObject activeBuilding;
public GameObject[] buildings;

void Update () {


public void BuildChoice(GameObject btnObj)
	string btnName =;    **<---- Here is the error**

	if (btnName == "Btn_Floor") 
		Debug.Log("Hit button Btn_Floor");
                    activeBuilding = buildings[0];


            if (btnName == "Btn_Wall") 
		Debug.Log("Hit button Btn_Wall");
                    activeBuilding = buildings[1];

I don’t want to have a function for every single button. I could always do that but thought that this might be a good way.

In the end I want to have each button represent a gameobject in an Array and have that gameobject in the array be active. The player can then place this gameobject on the map as part of a building game.

Instead of Using a custom GUI I manually added the buttons. AS fafase said. You can also create a method for every single button that you have.