- Home /
Question by
Proshanto-C · Feb 24, 2019 at 11:12 AM ·
texttextfield
Textbox not clearing properly
Hi I'm working on a program that generates a set of random numbers and prints them out to a textbox and adds them together. I have a button that is supposed to clear out the textbox and then generate new numbers to replace the old ones, however, whenever I press the button, the old numbers stay and new ones are added underneath them. How can I fix this?
Here is my current code:
public void Start()
{
viable = false;
sum = 0;
List<int> numbers = new List<int>();
userinp = 0;
randNumText.text = ""; //This is the line that is supposed to reset the text in the textbox
correctIncorrectPanel.SetActive(false);
resetButton.SetActive(false);
RandGen();
}
void RandGen()
{
for (int i = 0; i< 5; i++) //generate 5 random numbers
{
numbers.Add(RandomNumber(100, 1000));
sum = sum + numbers[i];
}
int[] numArray = numbers.ToArray();
string numString = string.Join("\n", numArray);
randNumText.text = numString;
}
public void ResetButton()
{
Start();
}
Comment
Try adding numbers.Clear() to the beginning of RandGen() function to clear the list. If you go in Debug mode with your current code you should see the list adding 5 numbers on top of the previous ones.
Your answer
