if(pressbutton) --> do something!!
I've got two "IU Buttons", so i want to change the text of "Button1" if i pressed "Button2" in JS. To do that, im trying with the following code:
#pragma strict
import UnityEngine.UI;
var hello : Text = transform.GetComponentInChildren(Text).GetComponent(Text);
function Start () {
hello.text = "text1";
}
function Update () {
if(Input.GetButtonDown("Button2")){
hello.text = "text2";
}
}
But it doesn't work. How can i do it?
Answer by andrei2699 · May 12, 2016 at 08:08 PM
if(Input.GetButtonDown("ButtonName")) returns true if one of the virtual buttons is clicked and not if one of the UI Buttons is clicked. The virtual buttons are configured in the Input Manager, which can be accessed by the name.
Every UI Button has a script called "Button" attached. This script contains an event called OnClick which can call a function from a script created by you. The created script must be attached to a gameobject.
For example: You create a script which contains a function called "ButtonClick()". This function should be put in the function slot from the UI Button, at the OnClick event.
#pragma strict
import UnityEngine.UI;
function ButtonClick()
{
hello.text = "text2";
}
For more information about the UI Button and the use of it, you should watch this video.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080609im_/https://answers.unity.com/themes/thub/images/avi.jpg)