- Home /
question about buttons and event onclick
Hi i'am new in unity 3D, and i work in a project and i need do the next things with buttons and i don't know how to do this.
i have a button empty, when i click the button change the text of the button to "X", in the next click i change the text to "O", in the next click change to "I" and finally in the next click return to button empty
i don't know how to program the event and the button.
What exactly are you asking for? Do you want the button to be blank and then change to the letter "X" or does X mark a word. Or do you want the button to do something like start the game. It may just be me but I am slightly confused.
ok now i understand the event onlick... i only have the code to change the text in te button. but i have the error in the line " public Text ButText; " the type or name space 'text' could not be find. why???
i have this code
public Text ButText;
int counter = 0;
public void changeText()
{
counter += 1;
switch (counter)
{
case 1:
ButText.text = "X";
break;
case 2:
ButText.text = "O";
break;
case 3:
ButText.text = "I";
break;
default:
counter = 0;
ButText.text = "";
break;
}
}
}
and i can't use the function in onclick(), because don't gave me the function changeText().
i don't understand... i attach the script ti all my buttons, and drop the combioBoton???
Answer by Ali-hatem · Apr 12, 2016 at 05:52 PM
Text ButText; // edited
int counter = 0;
void Start(){
ButText = GetComponentInChildren<Text>(); // add this line
ButText.text = "";
}
public void change(){
counter += 1;
switch (counter) {
case 1:
ButText.text = "X";
break;
case 2:
ButText.text = "O";
break;
case 3:
ButText.text = "I";
break;
default :
counter = 0;
ButText.text = "";
break;
}
}
i have this code... but i don't understand the public Text ButText; this variable is the name of my button or what is this? for example public button1 ButText; ???
using UnityEngine; using System.Collections;
public class botonOnClick : $$anonymous$$onoBehaviour {
public Text ButText;
int counter = 0;
void Start()
{
ButText.text = "";
}
void OnGUI()
{
Event e = Event.current;
if (e.button == 0 && e.is$$anonymous$$ouse)
{
Debug.Log("Left Click");
change();
}
else
if (e.button == 1)
Debug.Log("Right Click");
else
if (e.button == 2)
Debug.Log("$$anonymous$$iddle Click");
else
if (e.button > 2)
Debug.Log("Another button in the mouse clicked");
}
void change()
{
counter += 1;
switch (counter)
{
case 1:
ButText.text = "X";
break;
case 2:
ButText.text = "O";
break;
case 3:
ButText.text = "I";
break;
default:
counter = 0;
ButText.text = "";
break;
}
}
}
public Text ButText;
// ButText is the child object if the Button . & you asked for buttons and event onclick so why you use Event e = Event.current;
all you need is create onclick event on each button & call the change function & i already tested it in unity & it work so if you don't know how to use onclick see this tutorial make buttons do something
ok now i understand the event onlick... i only have the code to change the text in te button. but i have the error in the line " public Text ButText; " the type or name space 'text' could not be find. why???
i have this code
public Text ButText;
int counter = 0;
public void changeText()
{
counter += 1;
switch (counter)
{
case 1:
ButText.text = "X";
break;
case 2:
ButText.text = "O";
break;
case 3:
ButText.text = "I";
break;
default:
counter = 0;
ButText.text = "";
break;
}
}
}
and i can't use the function in onclick(), because don't gave me the function changeText().
delete the combioBoton object because it handles one button text so ins$$anonymous$$d we will use the buttons as game objects so attach the script to the buttons all the buttons will use the same script & on each button create On Click Event & drag & drop the each button to it's on On Click Event it's like what you have done with combioBoton object but we replaced it with the buttons . i see you tomoro it's 1 A$$anonymous$$ god night.
i don't know if i'am bad or stupid... i attach the script in the button and i add the button in the event onclick(). i select the option botonOnClick.changeText(); and mark the next error "nullReferenceException: Object reference not set to an object"
i drop the combioBoton.
Answer by Ryanless · Apr 19, 2016 at 11:30 PM
you have to make a reference to the ButtonText:
if you have public Text buttontext, then just drag and drop the text Gameobject on the field in the inspector. Or u could use ButText = GetComponentInChildren(); as mentioned. The good thing about that is it finds it automaticlly provided the script is on the button.