• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Worempie · Feb 02, 2015 at 03:46 AM · guigameobjectbutton

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 t$$anonymous$$s 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 t$$anonymous$$s gameobject on the map as part of a building game.

Comment

People who like this

0 Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tourist · Feb 03, 2015 at 04:51 PM 0
Share

btnObj is null. How do you set the function to be called (BuildChoice) to the button?

avatar image fafase · Feb 03, 2015 at 05:03 PM 1
Share

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.

Make 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.

My 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.

avatar image Worempie · Feb 03, 2015 at 06:34 PM 0
Share

@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.

avatar image Worempie · Feb 06, 2015 at 08:57 AM 0
Share

If you guys could post it as an answer I'll give you an upvote ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Feb 05, 2015 at 09:32 PM 0
Share

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 : MonoBehaviour {
 
     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);
     }
 }
avatar image Worempie · Feb 06, 2015 at 08:56 AM 0
Share

I was already wondering why I couldn't give any upvotes to comments. Quite new to answers in unity. Thanks for the tip :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Bullet Prefab not instantiating 1 Answer

Char select / GUI button question. 3 Answers

How do I make individual buttons change individual variables? 0 Answers

Detect Click on Gameobject 0 Answers

Cube click pop-up menu 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges