- Home /
Using Buttons to write numbers
Hello everyone. So I have 10 buttons labled with the numbers 0-9, I managed to let the buttons write their numbers into a textfield when I click on them (with the simple OnClick event implemented in unity), but everytime I click on a new button, the previous number gets overwritten and I need it to remain to be able to show numbers like 12, 34 and so one. Unfortunately I dont even know how to properly start the C# Script for that. I hope that some of you can help me. ^^
Answer by OneCept-Games · Jan 03, 2018 at 12:20 PM
When you assign the button value to your textfield add it instead of assigning it.
Like: textfield.text += string.Format("{0}", button.text);
Answer by tormentoarmagedoom · Jan 03, 2018 at 01:18 PM
Hello @TheNicojack :
As @OneCept-Games says, you can use this "formula"
FinalText.text = FinalText.text + NewText.text;
so the new characters will be writen after the old characters.
This "formula is exactly the same as this (short way)
FinalText.text += NewText.text;
This ( + = ) can be used with all variables like int or float, to add or remove ( - = )
you can also check the length of the string variable to decide if want to create a new string or continue adding the new text to the old with:
int NumberOfCharacters = FinalText.text.Length;
For remove characters from a string variable you need to use
string NewText = FinalText.text.Substring (int StartIndexCharacter);
More info in https://answers.unity.com/questions/202004/substrings.html
Good luck with your project!
Answer by TheNicojack · Jan 05, 2018 at 02:04 PM
@tormentoarmagedoom @OneCept-Games Thanks for the help ^^ now my code looks like this:
public class T1Btn : MonoBehaviour {
public Text FinalText;
public Text Btn1;
public Text Btn2;
void ButtonText()
{
FinalText.text = FinalText.text + Btn1 + Btn2;
}
}
but it isn't working, can you tell my hat I am doing wrong ?
You are assigning Text object tro a string. Always make sure you are assigning same Type's, except when you use the object type. And also always check for objects not assigned in the Inspector, like: Debug.Assert(Btn1 != null, "Btn1 is not assigned"); Your code should be:
FinalText.text = FinalText.text + Btn1.text + Btn2.text;
Alright thx for that ^^ now the only problem is, that I can't select my function in the inspector of the buttons. I attached the script on the $$anonymous$$ain Camera, added the text segments of the buttons aswell as the textfield ,where everything should get written in, in the Btn1, Btn2, Btn3 and so on fields in the inspector and added the main camera (containing the script) to the on click events of the buttons, but when I want to select the function I wrote, I can select the T1Btn function but the ButtonText function I wrote I can not select. Did I make a mistake there ?
P.S. I hope you understand what I mean ^^
Alright thx for that ^^ now the only problem is, that I can't select my function in the inspector of the buttons. I attached the script on the $$anonymous$$ain Camera, added the text segments of the buttons aswell as the textfield ,where everything should get written in, in the Btn1, Btn2, Btn3 and so on fields in the inspector and added the main camera (containing the script) to the on click events of the buttons, but when I want to select the function I wrote, I can select the T1Btn function but the ButtonText function I wrote I can not select. Did I make a mistake there ?
P.S. I hope you understand what I mean ^^
@TheNicojack You need to make ButtonText() public in order for you to be able to select it in the inspector ;)
You wrote it as:
void ButtonText () {
// Blah blah blah
}
To make it public it should be:
public void ButtonText () {
// Blah blah blah
}
I assume you're new to program$$anonymous$$g?
Your answer
Follow this Question
Related Questions
Remote KeyPad with Buttons 1 Answer
How Do I Make My Score Add Numbers Instead Of Strings? 2 Answers
Dividing On strings CS0019 1 Answer
How to connect the strings i get ftom pressing buttons 1 Answer
Moving buttons 1 Answer