- Home /
float.parse and webGL build
Hi All
I am having trouble with a webGL build. I get an incorrect formatExeption. I think I eliminated the problem to the parsing. How do I get the parsing to work in a webGL build?
Here is a snippet from my code:
noOfDebitsPerMonth = float.Parse(edtNoOfDebitsPM.GetComponent<TMP_InputField>().textComponent.text);
valuePerDebit = float.Parse(edtValuePerDebit.GetComponent<TMP_InputField>().textComponent.text);
noOfUnpaids = float.Parse(edtNoOfUnpaids.GetComponent<TMP_InputField>().textComponent.text);
noOfDisputes = float.Parse(edtNoOfDisputes.GetComponent<TMP_InputField>().textComponent.text);
interestRate = float.Parse(edtInterestRate.GetComponent<TMP_InputField>().textComponent.text);
interestImpact = float.Parse(edtInterestImpact.GetComponent<TMP_InputField>().textComponent.text);
Debug.Log(noOfDebitsPerMonth);
Debug.Log(valuePerDebit);
Debug.Log(noOfUnpaids);
Debug.Log(noOfDisputes);
Debug.Log(interestRate);
Debug.Log(interestImpact);
float totalNoOfUnpaidsAndDisputes = noOfDebitsPerMonth * ((noOfUnpaids / 100f) + (noOfDisputes / 100f));
lblTotalNoUD.GetComponent<TextMeshProUGUI>().text = totalNoOfUnpaidsAndDisputes.ToString();
The obvious potential cause is just that one of your text components contains a string that can't be parsed to a float.
I would suggest you switch to using TryParse ins$$anonymous$$d of Parse. It won't throw exceptions and you'll be able to test the bool it returns and then debug log the details if/when it fails. And this applies to any time when you're not in total control of the string - if you don't know for sure, at the time you're writing the code, that the Parse will succeed, then exceptions can happen and you should either use TryParse or add try/catch blocks.
Answer by mKleinIQ · Nov 29, 2017 at 09:53 AM
So I ended up doing it like this:
if (float.TryParse(edtNoOfDebitsPM.GetComponent<TMP_InputField>().text, out result))
noOfDebitsPerMonth = result;
float totalNoOfUnpaidsAndDisputes = noOfDebitsPerMonth * ((noOfUnpaids / 100f) + (noOfDisputes / 100f));
lblTotalNoUD.GetComponent<TextMeshProUGUI>().text = totalNoOfUnpaidsAndDisputes.ToString();
A word of warning - if you have a string that can't be parsed to a float (which is seems like you probably do), then that code will leave noOfDebitsPer$$anonymous$$onth unchanged, and will continue without warning you about it. It's possible that that's what you want, but more often you'll want to have an else
clause dealing with the fact that the TryParse failed (possibly just a Debug.LogWarning which shows you the the string that's failed)
Thanks, will add the 'else'. Luckily I see that Text$$anonymous$$eshPro's input boxes can be set to numbers only.
Your answer
![](https://koobas.hobune.stream/wayback/20220612143526im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
String Parse 1 Answer
Setting TerrainData heights to a float array? 0 Answers
MongoDb in Unity WebGL 0 Answers