- Home /
creating a label from a button click
I have one button, that will check if my textfield is filled. If it's not, it will create a label for me, but isn't working. So please guys, tell me why =).
A lil bit of my code:
contentOfTextField = createTextField(400, 200, 400, 30, contentOfTextField, 20);
if(createButton(620, 500, 100, 30, "Confirm")) {
if(contentOfTextField.Length == 0) {
createLabel(400, 100, 300, 30, "Please, put an user name!");
}
else {
confirm = true;
}
}
Thank you!
Well this is where debugging comes in handy. Use Debug.Log() or print() to get the values returned by contentOfTextField:
if(createButton(620, 500, 100, 30, "Confirm")) {
Debug.Log("The text field string equals: " + contentOfTextField.ToString() + " and has " + contentOfTextField.Length + " characters.");
}
Btw how are you creating the button and textfield?
the creation of the button it is a method:
public bool createButton(float left, float top, float width, float height, string content) {
return GUI.Button(new Rect((screenX-screenX)+left, (screenY-screenY)+top, width, height), content);
}
the same way in the textfield! I forgot of this Debug.Log :p. Thank you, i used that! He shows me the size of the textfield, but it isn't show the label that i want to create:
if(createButton(620, 500, 100, 30, "Confirm")) {
if(contentOfTextField.Length == 0) {
createLabel(400, 100, 300, 30, "Please, put an user name!");
Debug.Log("The caracters of the textfield:"+contentOfTextField.Length+"");
}
else {
username = contentOfTextField;
confirm = true;
showContentProfile = false;
Instruments.clickedConfirm = true;
Instruments.showContentInstrument = true;
}
}
Answer by FL · May 05, 2013 at 05:16 PM
Your way only display the label a single frame (or even less). Declare a global variable private var showLabel : boolean = false;
. Change your code to:
contentOfTextField = createTextField(400, 200, 400, 30, contentOfTextField, 20);
if(createButton(620, 500, 100, 30, "Confirm")) {
if(contentOfTextField.Length == 0) {
showLabel = true;
}
else {
confirm = true;
}
}
if(showLabel)
createLabel(400, 100, 300, 30, "Please, put an user name!");
that's right, i follow the same logic, but i want that label just appears when the user press the button and disappear when he leave's the button, and this fits better with RepeatButton! So thank you anyway.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101523im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
If button highlighted 2 Answers
A Button, A Boolean and 2 Textures. 2 Answers
Button Turns Off and On Object 1 Answer
Button then instanitates gameobject 1 Answer
How to hook up the particle system to jump animation? 1 Answer