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 Aresak · Aug 06, 2014 at 08:24 AM · c#inputgetcomponentbooleanbool

GetComponent, set boolean to true but it willn't revert

Hi, having a trouble. I'm trying it with easier way or even harder way but it's not working lel.

This code I'm having:

 if(Input.GetButtonDown("Interaction")) {
     if(ladder.GetComponent<Ladder>().isClimbing) ladder.GetComponent<Ladder>().isClimbing = false;
     if(!ladder.GetComponent<Ladder>().isClimbing) ladder.GetComponent<Ladder>().isClimbing = true;
 }


When I will touch button first, it will activate checkbox, change boolean to true but when I will trigger it again. It willn't set to false. Any idea?

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

3 Replies

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

Answer by Professor Snake · Aug 06, 2014 at 08:27 AM

This is basically two if statements one after the other. Both will execute, which means the first one makes sure isClimbing is always set to false and the second one is always true. Try something like

ladder.GetComponent().isClimbing = !ladder.GetComponent().isClimbing,

or

 if(ladder.GetComponent().isClimbing)
   ladder.GetComponent().isClimbing = false;
 else
   ladder.GetComponent().isClimbing = true;

if you want to learn how if/else statements work.

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 Aresak · Aug 06, 2014 at 08:33 AM 0
Share

Yeah I'm idiot :D I'm having these troubles always. I was about to use if/else.. Never$$anonymous$$d, your code is better and smaller so I will use yours. Thanks you for the tip!

avatar image
1

Answer by tanoshimi · Aug 06, 2014 at 08:28 AM

Look through your code. You don't do one line or the other, you do them both. So, if when you press the "Interaction" button isClimbing is true then the first line executes and sets it to false. The second line then executes and sets it straight back to true again.

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
avatar image
1

Answer by MaT227 · Aug 06, 2014 at 08:29 AM

You are simply doing both conditions that's why this is not working but here is a simplified code :

 if(Input.GetButtonDown("Interaction"))
 {
     ladder.GetComponent<Ladder>().isClimbing = !ladder.GetComponent<Ladder>().isClimbing;
 }

or

 if (ladder.GetComponent().isClimbing)
 {
     ladder.GetComponent().isClimbing = false;
 }
 else
 {
     ladder.GetComponent().isClimbing = true;
 }

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 Aresak · Aug 06, 2014 at 08:35 AM 0
Share

Yeah, I'm using the code on the top from now, thanks you for the tip.

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

23 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

Related Questions

What does this mean? 1 Answer

Scavenger Hunt List Bools Question 1 Answer

Bool not returning correct value from getCompoent 1 Answer

c# Ignoring conditional statement? 1 Answer

C# Boolean Constantly Changing 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