Search Unity

[SOLVED] How to check if bool is true from another script? [SOLVED]

Discussion in 'Scripting' started by Pixitales, Mar 20, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    I need help again, i am still new. How do you check if a bool = true, from a different script. Do you put it in the start function or update function? This is for when a ui button is pressed.
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    The simplest way is to make it a public member of the class it is in, like this:
    Code (CSharp):
    1. public class InputTracker : MonoBehaviour
    2. {
    3.     public bool IsPressed;
    4.  
    5.     void Update()
    6.     {
    7.         IsPressed = Input.GetButton("ButtonName");
    8.     }
    9. }
    Attach an
    InputTracker
    script to your
    GameObject
    and then obtain a reference to it from any other by getting it as a
    Component
    , like this:
    Code (CSharp):
    1. public class InputReader : MonoBehaviour
    2. {
    3.     public GameObject InputObject;
    4.  
    5.     InputTracker inputTracker;
    6.  
    7.     void Start()
    8.     {
    9.         inputTracker = InputObject.GetComponent<InputTracker>();
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         if (inputTracker.IsPressed)
    15.         {
    16.             // Do whatever.
    17.         }
    18.     }
    19. }
    But this would be kind of silly because you can query
    Input.GetButton
    from any code, without the need to save its state in a separate bool.

    Now, making member variables public in C# is frowned upon. Unfortunately, it's a fundamental aspect of writing C# code for Unity, so you will do it a lot. But you should limit public members to variables you need to access from the Unity Inspector. For others, use properties, like this:
    Code (CSharp):
    1. public class InputTracker : MonoBehaviour
    2. {
    3.     private bool isPressed;
    4.  
    5.     public bool IsPressed
    6.     {
    7.         get
    8.         {
    9.             return isPressed;
    10.         }
    11.  
    12.         set
    13.         {
    14.             isPressed = value;
    15.         }
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         IsPressed = Input.GetButton("ButtonName");
    21.     }
    22. }
    Note that I have used capitalization to distinguish between the private member
    isPressed
    and the public property
    IsPressed
    . Using capitalization to distinguish between variables in C# is also frowned upon, but doing it in this case (where the private member stores the state of a public property) is treated by some people as a legitimate exception, and I'm one of those people (others may see it differently).

    Read up a bit on "access modifiers" and "properties" and this will all make a lot more sense to you.
     
  3. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    I got an error on that get Input.GetButton..... says go to edit >> settings and add a button. Mine is actually a button on the screen. Not a button on the keyboard or mouse. Let me try the other part first.

     
    Last edited: Mar 21, 2019
  4. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227

    Thanks, it worked out perfectly. Keeps my script all organized.
     
    Stevens-R-Miller likes this.