I have been trying to find an aswer to this for hours. I am trying to get the button GameObject that accessed a function on one of my scripts. I just need to know which button accessed the script. Please help in UNITYSCRIPT!
Asked
Active
Viewed 2,540 times
0
-
First of all, how many different buttons that is accesing to script? – Thalthanas Jul 27 '17 at 06:14
-
So you want know to what button you need to press to invoke a function? – Ignacio Alorre Jul 27 '17 at 06:16
-
Paste the script here to check, I believe it will be easier – Ignacio Alorre Jul 27 '17 at 06:17
1 Answers
0
You can make a public string variable in the main script which will be hold the name of the button which accesed the script
public string clickedButtonName;
In buttons you are gonna add another script which will updates clickedButtonName
variable with GetComponent
Pseudo code
Button Script that will be called on Click;
public Gameobject yourMainScriptObject;
public void buttonClicked()
{
yourMainScriptObject.GetComponent<YourMainScript>().clickedButtonName = gameObject.name.toString();
}
So when a button is clicked they will update the string value on the main script with their name.
Hope this helps! Cheers!

Thalthanas
- 496
- 6
- 10
-
This solution is good enough for what I need. This will seriously cut a lot of lines out of my script. Thank you! – Andrew Friedman Jul 28 '17 at 00:13