Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
2
Question by Skyppex_ · Jul 23, 2021 at 07:15 PM · textmeshintstringsparse

How can I convert the TextMeshPro text to an int?

I'm having trouble trying to convert from string to int in unity. It keeps giving me FormatExceptions.

In my editor i have dragged a reference to the text object in my scene to the script itself. Its type is TMP_Text. Lets call textObject. When calling the ParseString Method i pass in the textObject.text property since its the string i'm looking for. I have confirmed this with a Debug.Log (which works fine btw)

What i've tried so far:

 private static int ParseString(string dayText)
     {
         int num = 0;
         num = int.Parse(dayText);
         return num;
     }

This throws a FormatException

 private static int ParseString(string dayText)
     {    
         int num = 0;
         if int.TryParse(dayText, out num);
             return num;
 
         throw new System.ArgumentException("Argument must be a number");
     }

Here the TryParse returns false and the ArgumentException is thrown.

 private static int ParseString(string dayText)
     {
         dayText = string.Format("{0}", dayText);
 
         int num = 0;
         num = System.Convert.ToInt32(dayText);
         return num;
 
         throw new System.ArgumentException("Argument must be a number!");
     }

Here I get another FormatException.

 private static int ParseString(string dayText)
 {
     dayText = string.Format("{0}", dayText);

     int num = 0;
     if (int.TryParse(dayText, out num);
         return num;

     throw new System.ArgumentException("Argument must be a number!");
 }

I tried using the formatting method which didn't work. Still throws the ArgumentException.

 private static int ParseString(string text)
     {
         string temp = "1";
         temp = temp + text;
         int num = 0;
         if (int.TryParse(dayText, out num);
             return num;
 
         throw new System.ArgumentException("Argument must be a number!");
     }

I tried some concatination. Still returns false and throws the ArgumentException.

And for the record i tried all the above methods with each of the converting Methods. the other two throws a FormatException instead of the ArgumenteException i wrote.

Additionally i made a test script and tried some of the same conversion using that TMP_Text reference again, but a completely new one, seperate from anything that was already in the scene. Still no conversion on the object, but works fine when i define my own strings somewhere in the class or even change them at runtime with a [SerializedField] private string text;

I tried passing in my own string defined in the method where i use ParseString() and it works as expected. I did the same, defining a field and a property in the class.

I'm starting to believe there is no way around this and i have to find an alternative. It might just be a problem

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

2 Replies

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

Answer by Skyppex_ · May 12 at 07:40 AM

This seems to be a very annoying issue. The solution to my problem ended up being to use the TMP_InputField directly rather than TMP_Text. For some reason the conversions work then. Its strange because all that does is get get the text stored in its child TMP_Text object… Regardless it did the job for me, maybe it will for you.

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
avatar image
0

Answer by Nazirzadeh · Sep 15, 2021 at 11:43 PM

Hi, instead of dayText use dayText.GetParsedText()

l mean:

int.TryParse(dayText.GetParsedText(), out num)

Comment
Add comment · Show 2 · 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 jeff13gaydos · May 12 at 01:20 AM 0
Share

I've tried this in 2021.3.1f1 and int.Parse functions were still not able to figure out TMP text. Here is my code:

 public void OnChangeWidth(TMP_Text text)
 {
         int value = 0;
         Debug.Log(text.GetParsedText());
         if (text.GetParsedText().Equals("1"))
             Debug.Log("Equals 1!");
         if(int.TryParse(text.GetParsedText(), out value))
         {
             Debug.Log("Parsed value: " + value);
         }
 }

And here is the output I get:

alt text

output.png (5.4 kB)
avatar image Skyppex_ jeff13gaydos · May 12 at 07:43 AM 0
Share

Hey i posted an answer that worked for me, check if it work for you as well :) Hopefully it does…

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

168 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 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

How to remove parts from a string 1 Answer

parsing a double to bigint also getting playerprefs bigint 0 Answers

Error FormatException: Input string was not in a correct format. 0 Answers

Help int.Parse "Input String was not in the correct format" PROBLEM 0 Answers

I cant Multiply integer from Input Field 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