Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ahmad_Talal · Jan 27, 2014 at 05:31 PM · guitextfield

Convert.ToInt32 error

Well I have this script which works fine but the problem is that I cant write in GUI.TextField. mean that I cant edit the value to convert that new value into int. When ever I try to write in the TextField I get error. FormatException : Input string was not in the correct format.

here is My Script...

 using System;

 void OnGUI()
 {
 Convert.ToInt32(GUI.TextField(new Rect(150,80,30,20),"0", 25));
 }
Comment
Add comment · Show 1
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
avatar image Ahmad_Talal · Jan 27, 2014 at 05:28 PM 0
Share

I use this one as well but still the error.

 using System;
 public string stringtoedit = "123";
 
 void OnGUI()
 {
 stringtoedit = Convert.ToInt32(GUI.TextField(new Rect(150,80,300,20),stringtoedit, 25));
 }

and this time the error is,, Can not implicitly convert "int" to "string"

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KellyThomas · Jan 27, 2014 at 05:36 PM

I suggest reading this documentation then running the following code and checking the console to see what the contents of the string are.

 void OnGUI()
 {
     string numberString = GUI.TextField(new Rect(150,80,30,20),"0", 25);
     Debug.Log(numberString);
     int numberInt = Convert.ToInt32(numberString);
 }


Once you have more information if you are still unable to get it working edit your question to include the exact contents of the string and we can provide further advice.

You may also find `Int32.TryParse` useful.

Comment
Add comment · Show 9 · 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
avatar image Ahmad_Talal · Jan 27, 2014 at 05:43 PM 0
Share

Thanks for the answering. your method wont work. But I figure that out. I want to share that.. here what I did...

 public float editedString;
 public string stringtoedit = "123";
 
 void OnGUI()
 {
 stringtoedit = GUI.TextField(new Rect(150,80,30,20),stringtoedit, 25);
 
 editedString = Convert.ToInt32(stringtoedit);
 }
avatar image Ahmad_Talal · Jan 27, 2014 at 05:53 PM 0
Share

ok I got One more problem with it. When ever I play the game, the string contain the numbers. right. But When as soon as I remove the number then the string became NULL and all the gui disappear.. So how do I make it working?? Thanks in advance

avatar image KellyThomas · Jan 27, 2014 at 11:46 PM 0
Share

Well that is to be expected! Convert.ToInt32() will throw an exception when it fails it's convertion.

When an exception occurs normal execution stops and either:

  1. jumps to a catch block

  2. halts the program

Fortunatly unity seems to run all of our user code (`Start()`/`Update()`/`OnnGUI()`/etc) in a try block, otherwise people would crash the unity IDE and loose their work all the time.

In your case you can either use a try/catch block to handle the exception yourself allowing you to continue executing the rest of your OnGUI() method. Or use `Int32.TryParse()` which allows for adaptive behaviour:

 public string stringtoedit = "123";
  
 void OnGUI()
 {
     stringtoedit = GUI.TextField(new Rect(150,80,30,20),stringtoedit, 25);

     int parsedInt;
     if(Int32.TryParse(stringtoedit, out parsedInt) {
         // TryParse succeeded, parsedInt holds the parsed number
     }
     else {
         // TryParse failed, parsedInt holds the default value: 0
     }
 }

As always Garbage In / Garbage Out if you pass a null string to a parsing fuction it will be unable to extract any meaningfull data.

avatar image Ahmad_Talal · Jan 28, 2014 at 04:55 PM 0
Share

Thank uuuuuuuuuuuuuuuuuuuuuuuu very very much. I want to bother you one more time, For the string I use this code to restrict my input to only numbers, I have this problem of decimals, when I write decimals in the textfield it shows this error, Input string was not in the correct format.

here is my code,

     public string stringtoedit = "123";
      
     void OnGUI()
     {
     stringtoedit = GUI.TextField(new Rect(150,80,30,20),stringtoedit, 25);
      
 stringtoedit = Regex.Replace(stringtoedit, "[^0-9 .]", "");
 
     int parsedInt;
     if(Int32.TryParse(stringtoedit, out parsedInt) {
     // TryParse succeeded, parsedInt holds the parsed number
     }
     else {
     // TryParse failed, parsedInt holds the default value: 0
     }
     }
avatar image Ahmad_Talal · Jan 28, 2014 at 04:56 PM 0
Share

I use Regex.Replace to replace all the character with the given ones. It shows this decimal error.

By the way thank you for your help. I really appreciate that.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to make floats in the textfield? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

GUI.Textfield backspace control + Button Style problem 0 Answers

GUI.Textfield to int ? 2 Answers

Setting Scroll View Width GUILayout 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