Input.anyKey wont detect by some reason

I have tried to make an options screen and got stuck at how to change one key to another…

I press the GUI button and then it wont detect if i hold any key down

if(GUI.Button(Rect(95,55,75,30), GLOBAL.keyForward))
{
	//start setNewKey function for the GLOBAL.keyForward
	//(GLOBAL.js -> static var keyForward = "W";)
	setNewKey(GLOBAL.keyForward);
	setKey = true;
}

function setNewKey(KEY)
{
	while(setKey)
	{
		if(Input.anyKey)   //Think my problem is here
		{
			KEY = Input.inputString;
			setKey = !setKey;
		}
	}
}

Full script:

var currMenu : int = 0;
var currOptMenu : int = 0;

/////////////////////////////////////////////////////////////////
var showOptions : boolean = true;
var optionsPos = Rect(Screen.width + 120, 20, 550, 350);

var setKey : boolean = false;

//Buttons etc. inside:
function DoOptions (windowID : int) {

	if(GUI.Button(Rect(10,20,125,30), "Game")) {
		currOptMenu = 0;
	}
	if(GUI.Button(Rect(145,20,125,30), "Video")) {
		currOptMenu = 1;
	}
	if(GUI.Button(Rect(280,20,125,30), "Audio")) {
		currOptMenu = 2;
	}
	if(GUI.Button(Rect(415,20,125,30), "Controlls")) {
		currOptMenu = 3;
	}

	switch(currOptMenu)
	{
		//~skipped case 0-2 since i have no probs here
		case 3: //Controlls
			GUI.Label(Rect(20,60,75,20),"Forward");
			if(GUI.Button(Rect(95,55,75,30), GLOBAL.keyForward))
			{
				setNewKey(GLOBAL.keyForward);
				setKey = true;
			}
		break;
	}
	
	if(setKey)
		GUI.Label(Rect(20,120,75,20),"switch");

	GUI.DragWindow ();
}

function setNewKey(KEY)
{
	while(setKey)
	{
		if(Input.anyKey)
		{
			print("Key was changed");
			KEY = Input.inputString;
			setKey = !setKey;
		}
	}
}


////////////////////////////////////////////////////////////////

function OnGUI()
{
// Open/close optionsBox
showOptions = GUI.Toggle (Rect (100,10,100,20), showOptions, "Window 0");

	if (showOptions)
		optionsPos = GUI.Window (0, optionsPos, DoOptions, "My Window");

}

i think the while() condition is your problem. you call the setNewKey function first, while setKey is probably still false and after that you set setKey to true, but by then, the setNewKey function is already finished because setKey was false

That’s one problem, yes; but fixing that will reveal another problem:

One you enter the while loop, you’ll stay there until the value of Input.anyKey changes. But since Unity is busy running your while loop, it never gets a chance to check for key presses and set that variable. You’ll loop forever.

What you need to do instead is set a flag to indicate you’re waiting for a key press (instead of calling setNewKey()) and then, in your DoOptions() function, test that flag – something like

function DoOptions() {
    if (waitingForKey  Input.anyKey)
    {
        key = Input.inputString;
        setKey = true;
        // do something with key
    } else {
        if(GUI.Button(Rect(95,55,75,30), GLOBAL.keyForward)) 
        {
            waitingForKey = true;
        }
    }
}

Also notice the comment ‘Do something with key’ – the code you have now just assigns the pressed key to the function-local KEY vaiable, which is then discarded when the function returns. You don’t actually store the key anywhere.

The main point, though, being that you need to avoid writing code that loops until a condition changes but does nothing within the loop to change the condition.

Thx for the help, i have noticed i do alot of loops and that’s my next thing to change, i am still a newbie in programming, but one have to learn it some day…

I’ll think i will manage to fix this with your help from now ;D

PS. The key is at tho moment stored for this time you play and will later be stored on either playerPrefs or what i hope, is to find another way

Found a way trough using strings and your post was alot of help.

Thx.