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 Hurag · Jun 15, 2016 at 12:41 AM · buttontextbooleanonclickarraylist

Array of buttons , and get their IDs

Okey, i'm making calculator and i want when you click on button 1 or whatever other number to put button text to my text that is named input. So i made this public :

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public class Glavnaskripta : MonoBehaviour {
     
 
     public Text unos3;
     public Button[] button;
     private string stringunosa;
     public Text rezultat;
 
     public void InputNumber()
     {
         int i;
         for(i = 0; i < 10; i++)
         {
             if(button[i].isActiveAndEnabled)
             {
                 string b = button[i].GetComponentInChildren<Text>().text;
                 unos3.text = b.ToString ();
             }
         }
     }

So basically i want to know which button is pressed and add his text to Input.

Any help?

You have picture down there how it looks.

capture.png (8.3 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by andrei2699 · Jul 25, 2016 at 11:05 AM

There are several ways to achieve what you want, but all involve the same method :

 public void OnButtonClick(int id)
 {
    // The id is the text of the button.
     unos3.text = (id+1).ToString();
 }

The first method is to do it by scripting:

 void Start()
 {
     for(int i=0;i<button.Length;i++)
     {
         button[i].onClick.AddListener(() => OnButtonClick(i+1));
     }
 }

The second way is to add the function in the inspector :

You choose a button, you look in the inspector at the Button component and there you will see OnClick. Finally, you will add the object that contains the script with the OnButtonClick method and choose the method.

Note: the buttons from the array should be ordered. The button with the number 1 should be first and the one with number 9 should be last.

For more information about the first way, visit this link.

For more information about the second way, visit this link.

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

65 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 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

Create game object from UI button click even 1 Answer

Display loading indicator during onClick event? 0 Answers

Check through array list 1 Answer

Button object has no attribute OnClick 1 Answer

Trying to increment a variable in a Coroutine on pressing a UI button 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