Closed. This question needs debugging details . It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem . This will help others answer the question.
Closed 5 days ago.
Improve this question
Code and Debug window You can clearly see that the value of my string variable scoreText is "9" but it can't be converted to an integer.
Here is the whole exception message but I don't see how that would help:
FormatException: Input string was not in a correct format. System.Number.ThrowOverflowOrFormatException (System.Boolean overflow, System.String overflowResourceKey) (at <9aad1b3a47484d63ba2b3985692d80e9>:0) System.Number.ParseInt32 (System.ReadOnlySpan 1[T] value, System.Globalization.NumberStyles styles, System.Globalization.NumberFormatInfo info) (at <9aad1b3a47484d63ba2b3985692d80e9>:0) System.Int32.Parse (System.String s, System.IFormatProvider provider) (at <9aad1b3a47484d63ba2b3985692d80e9>:0) System.Convert.ToInt32 (System.String value) (at <9aad1b3a47484d63ba2b3985692d80e9>:0) ScoreChanger.ChangeScore () (at Assets/ScoreChanger.cs:21) UnityEngine.Events.InvokableCall.Invoke () (at <86acb61e0d2b4b36bc20af11093be9a5>:0) UnityEngine.Events.UnityEvent
1[T0].Invoke (T0 arg0) (at <86acb61e0d2b4b36bc20af11093be9a5>:0) TMPro.TMP_InputField.SendOnEndEdit () (at Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMP_InputField.cs:3149) TMPro.TMP_InputField.ReleaseSelection () (at Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMP_InputField.cs:4146) TMPro.TMP_InputField.DeactivateInputField (System.Boolean clearSelection) (at Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMP_InputField.cs:4184) TMPro.TMP_InputField.OnDeselect (UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMP_InputField.cs:4196) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDeselectHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:120) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
I am using Convert.ToInt32(string) to convert it. However, it ends up throwing the exception "Input string was not in a correct format". I tried manually altering the value of scoreText to "9" and it works. How does that make sense?
1条答案
按热度按时间z2acfund1#
对不起,伙计们,我甚至没有说我到底在这里做什么,但我找到了解决办法:我在Unity中使用了Input Field UI元素,想读取它的内容并将其转换为整数。问题是Input field有一个包含组件TextMeshProUGUI的子对象。该对象存储文本,所以我假设我需要获取该组件,但结果是Input field有一个text属性,我只需要使用它。