Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by galatia420 · Oct 26, 2011 at 10:45 PM · variablesbooleangetkeydownif-else

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;

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image galatia420 · Oct 27, 2011 at 01:42 PM 0
Share

var is being declared at begenning of script outside of functions as:

static var HEALTHBARS : boolean = true;

avatar image aldonaletto · Oct 27, 2011 at 01:54 PM 0
Share

And how the other HEALTHBARS variable - the one that works - is declared?

avatar image galatia420 · Oct 27, 2011 at 02:05 PM 0
Share

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.

avatar image aldonaletto · Oct 27, 2011 at 03:28 PM 0
Share

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.

avatar image galatia420 · Oct 27, 2011 at 04:55 PM 0
Share

O$$anonymous$$G i feel stupid.... thank you so much four your patience and helping me find my error. i was using OnGUI() :)

Show more comments
avatar image
0

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;
}
}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image galatia420 · Oct 27, 2011 at 01:39 PM 0
Share

Still having the same results debug says the varriable is getting changed 4x but always ending back where it started.

avatar image
0

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;
             
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image galatia420 · Oct 27, 2011 at 01:41 PM 0
Share

added debug and script is changing var 4x but always ending back where it started.

avatar image
0

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;

 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Oct 27, 2011 at 12:25 PM 0
Share

This will not work because if h is pressed, the first IF will make it true, but the second will make it false.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can/how do I set up a boolean to continue an animation after button press 1 Answer

My button does not assign any variable value,My OnClick button works with Debug.Log, but doesnt assign any values it should 0 Answers

making variables work in two scenes 1 Answer

how to create boolean Varibles ? 3 Answers

Toggle between first and third person - how do I toggle back? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges