Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by TheAltbacker · Nov 19, 2015 at 12:17 PM · uibuttoncrafting

How to check which button was clicked from script.

Hi, I'm making a crafting system with the new UI system. It consists of a list of buttons representing the items to be crafted, and i created a script that contains the logic for changing the database(for all the craftables).alt text

 public class CraftManager : MonoBehaviour {
     public GameObject mainPanel;
     public Button[] buttons = new Button[17];
     public Text infoText;
     public Text remarkText;
 
 
     public void CraftStuff()
     {
    Debug.Log("OnClick reference worked!");
         if (gameObject == buttons[0])
         {
             Debug.Log("Button found, crafting stuff");
             if (PlayerStats.Scrap >= 2)
             {
                 PlayerStats.Scrap -= 2;
                 PlayerStats.Metal += 1;
             }
             else
             {
                 remarkText.text = ("Not enough resources.");
             }
         }
         if (gameObject.CompareTag("TimberButton"))
         {
             if (PlayerStats.Wood >= 2)
             {
                 PlayerStats.Scrap -= 2;
                 PlayerStats.Metal += 1;
             }
             else
             {
                 remarkText.text = ("Not enough resources.");
             }
         }

}

(This is the script I have so far)

The problem is this: I can not find a way to reference the correct part of the script when a certain button is pressed. (ie. When the button for ITEM 1 is pressed, the script should change the values of items like in the first IF statement of the script, for ITEM 2, it should do as in the second IF statement, and so on). I assigned the CraftStuff() method to execute when I click the button, (OnClick()), and it executes it(The first Debug string is displayed), but does not do anything else. The values of the items do not change, the second Debug string is not displayed(although the button is tagged properly). My problem is, that I cannot make out a way to differentiate between the buttons from script. I tried referring to them by tag, or making an array of buttons, but neither works.

If there is still not enough information, I will try to add more. I don't get any errors, it just simply does not do anything.

craftmenu.png (11.7 kB)
Comment
Add comment · Show 2
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 Dinosaurs · Nov 19, 2015 at 05:10 PM 0
Share

This question is very vague. Please be more specific about what the problem is and include any relevant code samples or errors you're getting.

avatar image TheAltbacker · Nov 19, 2015 at 10:03 PM 0
Share

@Dinosaurs : I added the necessary info. Hope it helps. Cheers.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dinosaurs · Nov 19, 2015 at 10:09 PM

You can either create 2 different methods (CraftWood() and CraftMetal() for example) and call them from the different buttons, or pass a parameter from the button when it's clicked.

The Unity UI tutorial explains how to do this https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI Button OnClick() Will not Trigger Function Call 0 Answers

C# On Click sound to play before loading scene 0 Answers

Help with buttons 0 Answers

unity 2018.3.12f1 Button OnClick call function on another instance? of script 0 Answers

How can I make mobile UI buttons fire when first touched rather than released? 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