# Is there a way to define a range of numbers in the place where an int would usually go?

I just discovered today that you can set Inputs to variable number keys with (Input.GetKeyDown(KeyCode.Alpha0 + [insert name of int here])). I have a list of bools (less than 10), and I wanted to do a thing where it would just be “whatever number key you press, that number item in the list of bools will be toggled.” Basically, like this…

``````if (Input.GetKeyDown(KeyCode.Alpha0 + ThisNumber))
BoolList[ThisNumber] = !BoolList[ThisNumber];
``````

Of course, the problem there is that “ThisNumber” has to be predefined. It doesn’t just recognize any number key you press and plug that into the variable. Granted, it’s easy enough to just say if (Input.GetKeyDown(KeyCode.Alpha1) {ThisNumber = 1}, and do that for each single-digit number. That probably would’ve even been easier than writing this post, but for the sake of general education, at least, is there a way to say something like this…

``````if (Input.GetKeyDown(KeyCode.Alpha0 + AnyNumberBetweenOneAndNine))
``````

For a simple and straightforward approach to this, you could run each input test individually in a for() loop:

``````// C#

// Cast KeyCodes as integers
int alpha0Key = (int)KeyCode.Alpha0;
int alpha9Key = (int)KeyCode.Alpha9;

void Update()
{
// Use integer conversions as a value range to test
for(int i = alpha0Key; i <= alpha9Key; i++)
{
// Cast the iterator as a KeyCode to return
// to the intended range
if(Input.GetKeyDown((KeyCode)i)
{
int arrayIndex = i - alpha0Key;
BoolList[arrayIndex] = !BoolList[arrayIndex];
}
}
}
``````

Edit: Whoopsie, forgot to subtract a value to get the array indices into a sane range.

Here is a small code to retrieve the integer off the input (simplified version at the end):

``````void Update ()
{
int n = Helper.GetDigitFromInput(Input.inputString);
if(n < 0) { return;}
// use n
}

// Helper.cs
public static int GetDigitFromInput(string str)
{
if (string.IsNullOrEmpty(str) == true) { return - 1; }
int n = -1;
if (int.TryParse(str, out n) == false) { return -1; }
return n;
}
``````

I made the method static so it can be placed in a helper class. If no input, string is empty and return -1. then try to convert to integer and if fails, return -1. Check if -1 to figure out if number. then use n in your array as you were.

Simplified version:

``````void Update ()
{
string str = Input.inputString;
if (string.IsNullOrEmpty(str) == true) { return; }
int n = -1;
if (int.TryParse(str, out n) == false) { return; }
// Use n
}
``````