- Home /
How to fix unstable boolean values? [possible bug]
Hello,
I am creating an override boolean value for a game project I am working on. Only when the "t" key is pressed does this override activate (Input.GetKeyDown), and it is reversed upon lifting the "t" key (Input.GetKeyUp). The problem is that the value seems to be toggling elsewhere in the code. It is very bizarre!
This is where it is called:
if (Input.GetKeyDown("t")){ objectTurret.GetComponent(turretOverlord).editModeGo(true); }else if (Input.GetKeyUp("t")){ objectTurret.GetComponent(turretOverlord).editModeGo(false); }
turretOverlord script functions
function editModeGo(isEditMode : boolean){
editOverride=isEditMode;
spinnerAccess.GetComponent(turretSpinShoot).setEdit(!editOverride);
}
function Update(){
if (!editOverride){
spinnerAccess.GetComponent(turretSpinShoot).setEdit(gamePlayer.GetComponent(playerMoveScript).editMode);
// playerMoveScript.editMode is ALWAYS true for this particular problem
}
}
turretSpinShoot.
private var editMode : boolean = false;
function Update () { print (editMode); }
function setEdit(modeOn : boolean){ editMode=modeOn; }
When I run this, the behavior changes each time I press/unpress "t": 1st time: "False", followed by an infinite stream of "True" 2nd time: a repeating stream of "False" then "True" then "True" 3rd time: a repeating stream of "False" then 4x "True" 4th time: a repeating stream of "False" then 6x "True" 5th time: a repeating stream of "False" then 8x "True"
What in the world could be causing this???!
Answer by Loius · Nov 23, 2010 at 04:33 AM
If your "if input" stuff is actually outside a function, you'll want to move it inside a function (probably update).
Additional:
Perhaps the problem is that GetKey expects a KeyCode argument. Try replacing "t" with KeyCode.T
Actually, that "if input" stuff is in an Update function. Sorry, forgot to copy that.
Ah, okay. The problem might be that Get$$anonymous$$ey uses $$anonymous$$eyCodes as parameters, and the string "t" might be interfering with its thinking.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084124im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array with boolean variables? 3 Answers
Change a Variable on Key Down 5 Answers
Some Boolean "Action" Trouble 2 Answers