- Home /
Change a Variable on Key Down
I've been trying this with no success :*( if anyone can help me find what im doing wrong that would help alot. Neither attempt I've tried to make is actually changing the variable, Healthbars is Always "false". at another part in my game i have a options menu with a click to toggle "HEALTHBARS" that is working just fine so my variable is working just cant get it to work on key press.
Attempt 1:
if(HEALTHBARS == false)
{
if(Input.GetKeyDown("h"))
{
HEALTHBARS = true;
}
}
else
{
if(Input.GetKeyDown("h"))
{
HEALTHBARS = false;
}
}
Attempt 2:
if(Input.GetKeyDown("h"))
{
HEALTHBARS = !HEALTHBARS;
}
Answer by aldonaletto · Oct 26, 2011 at 11:44 PM
The two codes should work, but the 2nd is much better. If this code is Unityscript, I suspect your problem has a different cause: probably the variable HEALTHBARS isn't being declared in this script, or is declared inside another function - in both cases the compiler will create a local variable called HEALTHBARS, which will not affect the original one.
If HEALTHBARS is declared in another script (the one that's working) you must use GetComponent to get a reference to the script, than access the variable with this reference. It's a tricky thing to do, specially if the other script isn't attached to the same object as this one. If HEALTHBARS is supposed to be global (a variable common to all scripts) you can declare it as static:
static var HEALTHBARS: boolean;
and access it in other scripts using ScriptName.HEALTHBARS. If the variable is declared in a script called HealthControl.js, for instance, you access it in other scripts like this:
HealthControl.HEALTHBARS = !HealthControl.HEALTHBARS;
var is being declared at begenning of script outside of functions as:
static var HEALTHBARS : boolean = true;
And how the other HEALTHBARS variable - the one that works - is declared?
its the same var just its a complicated script that turns them off and on depending on weather menus are open or not. (using var TUENHBBAC$$anonymous$$ON to change the HEALTHBARS var)
if i do the script:
if(Input.Get$$anonymous$$eyDown("h")) { HEALTHBARS = false; }
healthbars gets turned off. but if i do anythign else to make it chage the var on $$anonymous$$eyDown it changes 4x back to where it started. if i do $$anonymous$$eyUp it only changes the var 2x but still ends back where it starts.
This 4x thing smells like reading Input.Get$$anonymous$$eyDown inside OnGUI - OnGUI occurs several times during one Update cycle. Since Input.Get$$anonymous$$eyDown returns true when the key is pressed during all the update cycle, if you read it inside OnGUI you will have the same effect as pressing it several times.
O$$anonymous$$G i feel stupid.... thank you so much four your patience and helping me find my error. i was using OnGUI() :)
Answer by Gooch · Oct 27, 2011 at 12:45 AM
Make sure you check the status of var HEALTHBARS as well as the input.
{
if(Input.GetKeyDown("h") && HEALTHBARS == false)
{
HEALTHBARS = true;
}
else
{
if(Input.GetKeyDown("h") && HEALTHBARS == true)
{
HEALTHBARS = false;
}
}
Still having the same results debug says the varriable is getting changed 4x but always ending back where it started.
Answer by BalsamicVinegar · Oct 27, 2011 at 12:45 AM
Put a Debug.Log in your if statement and see if that is ever hit. If it doesn't hit, try using a keycode rather than a string. E.G.
if( Input.GetKeyDown( KeyCode.H)) {
HEALTHBARS = !HEALTHBARS;
}
added debug and script is changing var 4x but always ending back where it started.
Answer by TripodGRANNE · Oct 26, 2011 at 11:34 PM
Try this,
HEALTHBARS = false;
if(Input.GetKeyDown("h")) {
HEALTHBARS = true;
}
if(Input.GetKeyDown("h")) {
if(HEALTHBARS) {
HEALTHBARS = false;
}
}
This will not work because if h is pressed, the first IF will make it true, but the second will make it false.
Answer by galatia420 · Oct 27, 2011 at 01:51 PM
thankyou everyone :) but ... it hates me.
my var is being declared at the top of the script outside of functions as:
static var HEALTHBARS : boolean = true;
results of the debug:
Attempt 1: changed var 4x but always ended at what it started on(i.e true or false)
Attempt 2: code does not change the variable.
Attempt 3:
if( Input.GetKeyDown( KeyCode.H)) {
HEALTHBARS = !HEALTHBARS;
}
using keycode here it changes the var 4x so ame results as attempt 1
i have tried all other code thoughts posted here with the same results. swaping to KeyCodes isnt helping either :*( I found that changing to KeyUp only changes the var 2x