- Home /
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 = btnObj.name; **<---- 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.
btnObj is null. How do you set the function to be called (BuildChoice) to the button?
Here is my view, one method to rule them all is wrong. In the end, you are about to get a real method if you end up with a lot of buttons. I know because the app I am working on was done this way and it is a pain to refactor.
$$anonymous$$ake a method for each action, if you add a button, add a method. It makes it all clear and easy to maintain. Particularly if you have methods that do totally unrelated actions, it is even better to cut them into two classes to keep things tidy.
$$anonymous$$y advice, use the new uGUI system and drag each needed method in the onClick section.
Now if you want to keep your way, check the call of the method, if btnObj is null, you probably pass something wrong on the call.
@Tourist On the button I added the gameobject where the script is attached to and selected the function BuildChoice() from there.
@fafase You are right. I think i'll go with this approach. I thought I could make it like this, but with all the new buttons that I already want to add it's gonna be like you say a pain in the ass. Still thinking of going back to manually adding buttons in a GUI script.
If you guys could post it as an answer I'll give you an upvote ;)
Answer by Worempie · Feb 05, 2015 at 06:18 PM
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.
I appreciate this is answered, although as you did what @fafase suggested it might have been nice to ask them to make their comment an answer to credit them. Hey just a thought not trying to accuse you of anything :¬)
You could also try lambdas or delegates. Here's an example of a lambda:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LambdaButtons : $$anonymous$$onoBehaviour {
public GameObject prefabButton;
public RectTransform ParentPanel;
void Start () {
for(int i = 0; i < 5; i++)
{
GameObject goButton = (GameObject)Instantiate(prefabButton);
goButton.transform.SetParent(ParentPanel, false);
goButton.transform.localScale = new Vector3(1, 1, 1);
goButton.GetComponentInChildren<Text>().text = "Button " + i.ToString ();
Button tempButton = goButton.GetComponent<Button>();
int tempInt = i;
tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
}
}
void ButtonClicked(int buttonNo)
{
Debug.Log ("Button clicked = " + buttonNo);
}
}
I was already wondering why I couldn't give any upvotes to comments. Quite new to answers in unity. Thanks for the tip :)