The answers I've been looking for have not come.
How to count down by mouse click?
Hi, I have a code like this.
text = GUI.TextField(new Rect (10, 190, 80, 20), text);
text = Regex.Replace(text, @"[^0-9 ]", "");
I convert this code from string value to int value.
int i = string.IsNullOrEmpty(text) ? 0 : int.Parse(text);
num = Convert.ToInt32(i);
The codes work fine in this way.Later,
if(Input.GetMouseButtonDown(0))
{
num -= rock;
if (num <= 0)
{
num = 0;
Debug.Log("Nice Try!");
}
}
I want the number in the text box to be reduced when I click the mouse.The problem is that only one number can be reduced.
What do I need to do to reduce it to zero?
STR İNT This is what I want.
1.Click ===>> [5] => [4]
2.Click ===>> [5] => [3]
3.Click ===>> [5] => [2]
4.Click ===>> [5] => [1]
5.Click ===>> [5] => [0]
STR İNT But it happens this way.
1.Click ===>> [5] => [4]
2.Click ===>> [5] => [4]
3.Click ===>> [5] => [4]
4.Click ===>> [5] => [4]
5.Click ===>> [5] => [4]
6.Click ===>> [5] => [4]
7.Click ===>> [5] => [4]
/ / /
/ / /
50.Click ===>> [5] => [4]
Answer by PizzaPie · Apr 04, 2017 at 12:19 PM
First you say: "I want the number in the text box to be reduced" and then Under the "This is what I want" you present you only want to reduce the int so in case you want to reduce the text, according your first statement, something like this should work:
string text = "5";
void ReduceNum()
{
if (Input.GetMouseButtonDown(0))
{
int num = string.IsNullOrEmpty(text) ? 0 : int.Parse(text);
num--;
text = num.ToString();
if(num<=0)
Debug.Log("Nice try!");
}
}
Of course in that way everything is based on the text and the num does NOT pesists. There are better ways to achieve this but with the info you provide i can't guide you further than that. Anyway the problem in your code is that you never update the text.
Note: You don't need the line num = Convert.ToInt32(i); as int.Parse does already return an Int32