Search Unity

Argument is out of range??

Discussion in 'Scripting' started by besuser, Nov 19, 2007.

  1. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    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";
    }
    }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  3. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You could accept any input and just not parse the non-numeric stuff. Otherwise, you could do:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.     private string tmpstr = "";
    6.     private char nullChar;
    7.  
    8.     void OnGUI () {
    9.         if (Event.current.character < "0"[0] || Event.current.character > "9"[0]) {
    10.             Event.current.character = nullChar;
    11.         }
    12.         tmpstr = GUI.TextField (new Rect (275, 148, 30, 20), tmpstr);
    13.     }
    14. }
    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
     
  5. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    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.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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