- Home /
Keyboard Input Problem
Hi everyone !
I have a problem that I need help with. I want assign number 1,2,3 on the keyboard as hotkeys and whenever I press one of them, 3 objects 1,2,3 should have their materials changed accordingly to 3 sets of materials.
To make this easier to understand, for example when I press the "1" button on my keyboard, Object 1, Object 2 and Object 3 should have their materials changed to the material number 1 in their own material array. Below is my script to detect input and change the materials:
var materials_wall_left: Material[];
var materials_wall_right: Material[];
var materials_wall_top: Material[];
var getInput: String;
function Update()
{
if(Input.anyKey)
{
ChangeWallMaterial();
}
}
function ChangeWallMaterial()
{
getInput = Input.inputString;
var count:int;
if(int.TryParse(getInput,count))
{
if(count > materials_wall_left.length)
Debug.Log("Invalid choice");
else if(Input.GetButtonUp(""+ getInput))
{
GameObject.Find("build_wall_left").renderer.material =
materials_wall_left[count-1];
GameObject.Find("build_wall_right").renderer.material =
materials_wall_right[count-1];
GameObject.Find("build_wall_top").renderer.material =
materials_wall_top[count-1];
Debug.Log(count);
}
}
}
Now my problem is that my keyboard doesn't seem to work with this. I tried 1,2,3 but nothing changed. When I tried pushing 4, the Log "Invalid choice" popped up.
The interesting thing is that when I try to run this from another computer via Team Viewer, I can change objects' materials just fine, no problems at all. After that, I came back to the first computer and tried using a virtual keyboard, and it works. So I wonder what's the problem with my physical keyboard, or the script itself ? Oh, and I tried plugging in 4-5 other keyboards, and tried this scene on 3 another computers. Yes, and I got the same result everytime. If you've encountered this problem before or know how to solve it, please tell me ! Thank you very much !
Note: my Unity version is 3.2 pro.
Answer by keld-oelykke · Jun 06, 2011 at 08:38 PM
"else if(Input.GetButtonUp(""+ getInput))" is addressing logical buttons defined in the input manager ('Edit'->'Project Settings'->'Input')
... have you defined 1, 2 and 3 in there? or didn't you really mean to use the input manager functionality?
However, my guess is that it all works if you replace
"else if(Input.GetButtonUp(""+ getInput))"
with
"else if(0 < count)"
Hope it helps