Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by bhavinpanara22 · Feb 24, 2015 at 03:15 PM · unity4.6

How can i detect, which button was clicked ?

Hello,

I have three buttons in the scene. All of them are using same function when they are clicked. This function's script is applied on Main Camera. But how can i know, that which button was clicked ?

Thanks.

Comment
Add comment · Show 10
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 DGKN · Feb 24, 2015 at 03:23 PM 0
Share

Declare public variables for each button and assign them from the inspector ?

avatar image bhavinpanara22 · Feb 24, 2015 at 03:25 PM 0
Share

where i should declare them & can you please tell me the full process ?

avatar image DGKN · Feb 24, 2015 at 03:27 PM 0
Share

Could you show me your code so that I can help you ?

avatar image bhavinpanara22 · Feb 24, 2015 at 03:29 PM 0
Share

i don't know any coding for this problem. That's why i posted here. If i will get just one line of code, then i will grab other things from unity documentation.

avatar image Mmmpies · Feb 24, 2015 at 03:47 PM 1
Share

Are you using onClick to call the script? If so set the function to recieve a string or an int and set each button to send a different string or int. That way the function will know what button got clicked and act accordingly EDIT Actually that's what @DG$$anonymous$$N suggested, sorry reading on my phone so keep missing bits scrolling back and forth.

Show more comments

2 Replies

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

Answer by bhavinpanara22 · Feb 25, 2015 at 04:55 AM

Here we go. I have found the solution,

http://docs.unity3d.com/460/Documentation/ScriptReference/EventSystems.EventSystem-currentSelectedGameObject.html

Comment
Add comment · Show 4 · 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 25, 2015 at 08:49 AM 1
Share

Good that you got the Answer you wanted and even better you found it yourself in the documentation.

For the record, as you asked, to get a function accept a string open the script with the function and add a string variable in the () so:

 public void $$anonymous$$yOnClickFunction(string $$anonymous$$yString)
 {
     Debug.Log($$anonymous$$yString);
 }

And you can change the string for each button as a box appears in the OnClick area for you to enter whatever string you want for each.

Now this is trivial in this case as you already have an answer but lets say you give your function a bool (bool $$anonymous$$yBool) and you put that script on a toggle, which is effectively a bool itself.

When you select $$anonymous$$yScriptName -> $$anonymous$$yOnClickFunction you'll see two different versions in the list for $$anonymous$$yOnClickFunction. One with a bool and one without, if you select the one without a bool it passes back the value of the toggle directly. Very handy and worth knowing.

avatar image fehrhassan · Sep 08, 2016 at 08:50 AM 0
Share

this is the documentation for the new version http://docs.unity3d.com/540/Documentation/ScriptReference/EventSystems.EventSystem.html

and here is an example

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class Controller : $$anonymous$$onoBehaviour {
 
     public Button[] button;
 
 Public void OnClickedButton(){ 
 Vector2 thisPosition = EventSystem.current.currentSelectedGameObject.GetComponent<Transform>().localPosition;
 Debug.Log(thisPosition);
 }
 }
avatar image youssefghandour · Dec 15, 2017 at 07:25 PM 0
Share

how did you solve it plz

avatar image GregoryFenn · Nov 05, 2018 at 10:51 AM 0
Share

404 Error -- it's not there :(

avatar image
0

Answer by salvador007 · Feb 25, 2015 at 05:01 AM

Suppose you have two buttons say Btn1 ,Btn2 and you have written following code

 if(GUI.Button(btnposition and size here,"Btn1"))//this is Btn1
 {
 func1(); //you call this function on button click
 }
 
 if(GUI.Button(btnposition and size here,"Btn2"))//this is Btn2
 {
 func1(); //you call same function on button click
 }

Now to detect which button was clicked you can take two different boolean variables for these buttons click detection and use them as following

 if(GUI.Button(btnposition and size here,"Btn1"))//this is Btn1
 {
 boolBtn1 = true;
 boolBtn2 = false;
 
 func1(); //you call this function on button click
 }
 
 if(GUI.Button(btnposition and size here,"Btn2"))//this is Btn2
 {
 boolBtn1= false;
 boolBtn2=true;
 func1(); //you call this function on button click
 }

Hence true boolean will tell you here which btn was clicked. like if boolBtn1 true it will signify that Btn1 was clicked.

There could be other methods as well to achieve this but for the time you can use this.

Comment
Add comment · Show 4 · 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 bhavinpanara22 · Feb 25, 2015 at 05:04 AM 0
Share

You tried best to explain, but this is old GUI system & i am looking to implement this in Unity 4.6 UI. Anyway, i have solved my problem. Thanks for your effort.

avatar image salvador007 · Feb 25, 2015 at 05:23 AM 0
Share

i thought you are trying with old system.

avatar image ImFromTheFuture · Sep 27, 2016 at 09:28 AM 0
Share

The logic still remains the same with the new UI or the old.

avatar image youssefghandour · Dec 15, 2017 at 07:22 PM 0
Share

how did you solve it i have same problem plz

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

28 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

Related Questions

Unity 4.6: Position jumps when switching Canvas render mode from Overlay to camera in code 0 Answers

Changing the text component via script? 1 Answer

OnCollisionEnter2D 0 Answers

Issue with UI button positioning 1 Answer

Weird Unity buttons.Sometime works, sometime doesn't work. 11 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