- Home /
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.
Declare public variables for each button and assign them from the inspector ?
where i should declare them & can you please tell me the full process ?
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.
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.
Answer by bhavinpanara22 · Feb 25, 2015 at 04:55 AM
Here we go. I have found the solution,
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.
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);
}
}
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.
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.
The logic still remains the same with the new UI or the old.