Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Apr 06, 2017 at 11:12 AM by ikiyuzbirtv for the following reason:

The answers I've been looking for have not come.

avatar image
0
Question by ikiyuzbirtv · Apr 04, 2017 at 02:57 PM · guiconverttextfieldintegerstrings

How to count down by mouse click?

alt text

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]

help.png (85.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save whats in GUI.Text field to variable 1 Answer

Working with the GUI 4.6 (textfields and buttons) 1 Answer

Are TextFields in GUI.window generally uneditable? 0 Answers

Replacing a selected part of string in a GUI.Textfield 1 Answer

Convert int to string and vice-versa 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges