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 /
  • Help Room /
This question was closed Nov 03, 2015 at 01:36 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Ochreous · Nov 03, 2015 at 12:20 AM · c#booleanstuckfalse

C# Boolean Stuck as False

I have a bool in my script that is stuck as false. I tried changing if(clickCheck) to if(clickCheck == true) but it's still stuck as false. I believe this is due to the if statement being bypassed somehow. I have clickCheck set to false in the inspector so I don't know why it's being bypassed.

 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.EventSystems;
 using UnityEngine.Serialization;
 using UnityEngine.UI;
 using System.Collections;
 public class KeyPressString : MonoBehaviour, IPointerClickHandler {
     public Text text;
     public bool clickCheck;
     public PressedStringEvent OnPress;
     void Update(){
         if(clickCheck == true){
             if(Input.inputString != null){
             OnPress.Invoke(text.text);
             clickCheck = false;
             }
         }
     }
     public void OnPointerClick(PointerEventData eventData){
         clickCheck = true;
     }
 }
 [System.Serializable]
 public class PressedStringEvent : UnityEvent<string>{}

Comment
Add comment · Show 3
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 meat5000 ♦ · Nov 03, 2015 at 12:44 AM 0
Share

Use Debug.Log inside the if to check it.

avatar image Ochreous meat5000 ♦ · Nov 03, 2015 at 01:00 AM 0
Share

Ok, I found out from the Debug.Log that clickcheck becomes true for a millisecond and goes back to false. If I remove clickcheck = false then the bool isn't stuck. So clickcheck is stuck as being assigned. This might be due to how the functions run but I'm not sure.

avatar image meat5000 ♦ Ochreous · Nov 03, 2015 at 01:09 AM 0
Share

yes your routine is

 if (bool)
 {
     bool = !bool;
 }

if you expected something else to happen between but it doesnt it means that Input.inputString == null, surely?

This is a good example of a 'OneShot' routine. It fires once then switches itself off so it doesnt run again until told to. On->DoStuff->Off

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Nov 03, 2015 at 01:14 AM

As far as i know "Input.inputString" is never "null". It's most likely an empty string.

In general it's safer to use string.IsNullOrEmpty

      if(clickCheck){
          if(!string.IsNullOrEmpty(Input.inputString)){
              OnPress.Invoke(text.text);
              clickCheck = false;
          }
      }



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

Follow this Question

Answers Answers and Comments

31 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

Related Questions

My Bool function is returning False but it should be true in my opinion. Csharp C# 0 Answers

how do i check if all booleans in an array are false? C# 2 Answers

Automatic Switching of two or more booleans in Inspector 1 Answer

error CS0266 Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 2 Answers

Calling Bool in Network Command Not Working 0 Answers


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