Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Sep 07, 2018 at 01:16 PM by Mario8664 for the following reason:

Solved

avatar image
0
Question by Mario8664 · Sep 02, 2018 at 03:27 PM · buttonboolean

A bool is equal to the opposite of itself dosen't work

     if (Input.GetButtonDown("Look"))
     {
         look = !look;
     }

It doesn't work. I have tried

     if (Input.GetButtonDown("Look"))
     {
         look = true;
     }

This works.. Anyone know why?

Comment
Add comment · Show 4
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 ShadyProductions · Sep 02, 2018 at 04:36 PM 0
Share

Why wouldn't it work? It should switch between true/false eachtime u press button.

avatar image JVene · Sep 02, 2018 at 07:49 PM 0
Share

I have tried, too, and it does work.


What is required, now, is to detail how you came to the conclusion you did, and compare that to other methods for deter$$anonymous$$ing this (I detail $$anonymous$$e below), to see what you've observed different behavior.


Using Visual Studio 2017, the following was tested using the debugger:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 
 using System.Text;
 using System.Threading.Tasks;
 
 
 namespace ConsoleApp2 
 {
 
     class bcheck
      {
       public bool b = false;
 
       public void check() 
         {
          int n = 100;
 
          while( n > 0 )
             {
              b = !b;
              --n;
             }
 
         }
 
      }
 
 
     class Program 
     {
         static void $$anonymous$$ain(string[] args) 
         {
          bcheck bc = new bcheck();
 
          bc.check();
         }
     }
 
 }




This code uses a VS 'starting' console application to run a test. Within the loop the debugger was used to step through the code one line at a time, and the value of b was toggled between true and false (and back to true and the again to false) on every occasion exactly as expected.

avatar image Pinkuboxu · Sep 03, 2018 at 02:43 AM 0
Share

It would probably be useful to have the rest of your code. Personally I would use "look ^= true" to flip a boolean to it's opposite state, but that may give you the same results depending on what is happening.

avatar image toddisarockstar · Sep 03, 2018 at 04:31 AM 0
Share

its pry not the bool asignment its pry something with your button. try this to confirm:

       if (Input.GetButtonDown("Look"))
          { print("if you dont see this in the console your click is not working");
              look = !look;
          }
 

1 Reply

  • Sort: 
avatar image
2

Answer by JDelekto · Sep 03, 2018 at 09:25 AM

This code does indeed work; however, it may not work as you intended.

You first example flips the boolean value each and every time the Input.GetButtonDown("Look") returns true.

So, if look is initially false, the first time the button is pressed and released it will become true, the second time it is pressed and released it will become false, etc. and will continue to toggle back and forth between true and false with each subsequent button press.

If you expect the value to stay and remain true, then you need to do what you have done in your second example.

Comment
Add comment · Show 2 · 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 Mario8664 · Sep 07, 2018 at 01:08 PM 0
Share

According to the Unity APIs, it should works just in one frame. I press the button, and it changes once. Actually what I want is to flips the boolean value each and every time, but it doesn't work. It stills remains what it was at the beginning.

avatar image misher Mario8664 · Sep 07, 2018 at 01:14 PM 0
Share

Add Debug.Log("look bool is flipped to " + look); At least, you will check if your code ever tun and how many times.

Follow this Question

Answers Answers and Comments

112 People are following this question.

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

Related Questions

Unable to show gameobjects at specific timing 0 Answers

determine the minimum time for the player to hold the button 1 Answer

Can anyone help me in Bolt 0 Answers

My first if statement is going through but then the gui button doesn't work afterwards. Can anyone explain or solve it please(bool broke)? -1 Answers

my button dosent work, became a light switch instead of a normal button (like a bell) 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