I'm trying to figure out why unity is returning an error for my script. The error is "Argument is out of range". I know the script below may not make much sense as to what I'm doing. Just know that this is just a test. When I run the script below, I'll attempt to change the data in the text field by pressing a letter, then press the backspace button. This is when I get the out of range error. Any unity gurus out that may have any ideas? Thanks. using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { private string tmpstr = ""; void OnGUI () { tmpstr = GUI.TextField (new Rect (275, 148, 30, 20), tmpstr); tmpstr = "22"; } }
I'd say Unity is getting confused when you override the user's input by setting the string to 22. My guess is that the string length doesn't match the actual string anymore when you try to delete something like that, so Unity's probably doing the equivalent of referring to element 2 of a 1-element array. --Eric
Thanks Eric. I definately think you're right, but do you know if there's a way around this? What I'm trying to do is limit the input to only numbers in the field. That's why I'm reassigning the string.
You could accept any input and just not parse the non-numeric stuff. Otherwise, you could do: Code (csharp): using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { private string tmpstr = ""; private char nullChar; void OnGUI () { if (Event.current.character < "0"[0] || Event.current.character > "9"[0]) { Event.current.character = nullChar; } tmpstr = GUI.TextField (new Rect (275, 148, 30, 20), tmpstr); } } If you have any input in the GUI that needs non-numeric characters, then it would have to be above that if statement. (Speaking of which, the nullChar thing is kind of a hack...not sure how to set a char to null. I would have figured just using null, but that doesn't work with chars...no doubt I'm missing something obvious....) --Eric
That did the trick. I changed the nullchar line to this: Event.current.character = (char) 0; That seem to work like I wanted it. Thanks for you help.
Aha...good to know that. I found out that in Javascript, I can do "Event.current.character = "\0"[0];" to set a null char. --Eric