GetKeyDown code turned into a int issue [SOLVED]

Hello everybody!

I was trying to do a simple invent system, and it became very painful haha. Here’s what i’ve trying to do:

if(Input.anyKeyDown) {
			//this will take the keys in the ASCII format, 49 -> 1, 50 -> 2
			var e = System.Enum.GetNames(typeof(KeyCode)).Length;
			for(int i = 0; i < e; i++) {
				if(Input.GetKeyDown((KeyCode)i)) {
					actualWeapon = (GameObject)Instantiate (weapons[Convert.ToInt32(i)], 
															transform.position + (weaponLocation.position - transform.position), 
				//setting parent of the weapon
				actualWeapon.transform.parent = transform;
				Debug.Log ((int)i + " <- (int)key ---> " + (KeyCode)i + " <--- e -> " + e); 

I wanna to instantiate the weapon according to the key pressed which can be 1 to 9. I know about the other issues like if the user press F, but focusing i wanna know how to “convert” the KeyCode to one int, cause everytime i try i it returns me the ASCII code like 1 turns into Alpha1 in KeyCode and in ASCII turns into 49.

Any help will be appreciated, thank you!

If I understand your question correctly, there are two ways for you to approach this problem. The first is to use the string version of Input.GetKeyDown():

if (Input.GetKeyDown(i.ToString())) {

I don’t know if keycodes are guaranteed to map ASCII values, but this should work for most (if not all) platforms as well:

if (Input.GetKeyDown(KeyCode.Alpha0 + i))