• 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 krisfire · Sep 26, 2014 at 10:55 AM · bool

help with on off toggle. (c#)

I have it where a light turns on and off using getkeydown and two diffrent keys but I need it to do that with only one key and I know you need to use bool to do it but I'm not sure how to use bool, I've looked around and still can't figure it out. So if anyone can help that would be awesome.

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

2 Replies

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

Answer by Skierz · Sep 27, 2014 at 12:23 AM

If your still struggling this code should work:

  if (Input.GetKeyDown(KeyCode.E)){
             //toggle the light
             GetComponent <Light> ().enabled = ! GetComponent <Light> ().enabled;
 }
 

This works because you are setting GetComponent ().enabled to the opposite of what it currently is. A bool (short for Boolean) is a true or false variable. And GetComponent ().enabled is a Boolean so when called it will return it's current state. The '!' Operator just means not, so if the light was enable you are calling set the light to not true.

Sorry it's a bit wordy and the code might not be formatted well but I'm on my phone

Edit: oh and now thinking about it this probably isn't a good way of going about it as it will toggle straight back if the key is held down for more than one update so an event driven method would be better (but I'm new to unity so I'm not sure how it would be implemented)

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 krisfire · Sep 27, 2014 at 12:59 AM 0
Share

Thanks dude you just fixed the entire problem. and no it doesnt toggle back it only changes when the key is pressed. thanks again!

avatar image
1

Answer by 767_2 · Sep 26, 2014 at 11:04 AM

 private bool lightBool=true;
 
  if (Input.GetKeyDown("space")){
                 if(lightBool)
                  {
                   //turn off the light
                   lighBool=!lightBool;
                   }
                else
                 {
                   //turn on the light
                   lighBool=!lightBool;
                  
                  }
                       
           }  
         






Comment
Add comment · Show 3 · 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 DeveshPandey · Sep 26, 2014 at 11:31 AM 0
Share

No need of if else in this condition.

private bool lightBool=true;

  if (Input.Get$$anonymous$$eyDown("space")){
      lighBool=!lightBool;
 }
avatar image 767_2 · Sep 26, 2014 at 11:44 AM 0
Share

the if and else is for him to adds his light turning script that i commented and he will fill them later

avatar image krisfire · Sep 26, 2014 at 11:07 PM 0
Share

thanks for the answers but Im still not sure on how to use bool or the context its used in. if it helps heres the script I have now. // Update is called once per frame void Update () { if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)) //turn off the light GetComponent <Light> ().enabled = true; if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q)) //turn on the light GetComponent <Light> ().enabled = false; } } and probably should have said this in the original post Im still a noob at c#.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Slerpy Spider Won't Behave (Chaining Slerps) 1 Answer

Quick question about arrays and MonoDevelop 1 Answer

NullReferenceException: When accessing bools from another game object's script. 1 Answer

Problem with creating static bool... 1 Answer

Wait for bool to be true then return 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges