# Hide Variable Depending on Bool

**URL:** https://discussions.unity.com/t/hide-variable-depending-on-bool/663368
**Category:** Editor Extensions
**Tags:** Scripting
**Created:** [May 2, 2017, 10:07am UTC](https://discussions.unity.com/t/hide-variable-depending-on-bool/663368 "2017-05-02T10:07:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![TaleOf4Gamers1](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@TaleOf4Gamers1](https://discussions.unity.com/u/TaleOf4Gamers1)
#### Post date: [May 2, 2017, 10:07am UTC](https://discussions.unity.com/t/hide-variable-depending-on-bool/663368/1 "2017-05-02T10:07:22Z")

</div>

Hey,  
There are many posts on this and a couple with full scripts. However, one I tried that used an attribute just would not work. ([Hiding or Disabling inspector properties using PropertyDrawers within Unity - Brecht Lecluyse](http://www.brechtos.com/hiding-or-disabling-inspector-properties-using-propertydrawers-within-unity-5/)) This one would give me a type or namespace error for the ConditionalHideAttribute class no matter where I positioned the scripts. (Editor, not editor, one in editor, none in editor).

What I have at the minute is:

```csharp
using UnityEditor;

[CustomEditor(typeof(CameraMovement))]
public class CameraMovementEditor : Editor
{
    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();

        CameraMovement cam = (CameraMovement)target;

        cam.edgePan = EditorGUILayout.Toggle("Edge Pan", cam.edgePan);

        if (cam.edgePan == true)
        {
          // Enable distFromEdge field 
          // Enable panSpeed field 
        }
    }
}

```

The issue I am having is someone said on another persons post “if it is true, enable the fields”. But, how?  
All / any help would be appreciated.  
Thanks.

---

<div class="post-metadata">

### Author: ![Malleck666](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/malleck666/32/190393_2.png) [@Malleck666](https://discussions.unity.com/u/Malleck666)
#### Post date: [May 2, 2017, 10:58am UTC](https://discussions.unity.com/t/hide-variable-depending-on-bool/663368/2 "2017-05-02T10:58:30Z")

</div>

> **[Hide/Show properties dynamically in inspector.](https://discussions.unity.com/questions/192895/hideshow-properties-dynamically-in-inspector.html)**
>
> Here’s the thing. How do I hide/show items in the inspector tab dinamically, like iTween? When I select ‘MoveBy’, it loads all the properties available to the ‘MoveBy’ function. When I change it to something else, it unloads the properties (hides...

Something along these lines?

---

<div class="post-metadata">

### Author: ![TaleOf4Gamers1](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@TaleOf4Gamers1](https://discussions.unity.com/u/TaleOf4Gamers1)
#### Post date: [May 2, 2017, 12:08pm UTC](https://discussions.unity.com/t/hide-variable-depending-on-bool/663368/3 "2017-05-02T12:08:33Z")

</div>

> [@Malleck666](#):
>
> [http://answers.unity3d.com/questions/192895/hideshow-properties-dynamically-in-inspector.html](http://answers.unity3d.com/questions/192895/hideshow-properties-dynamically-in-inspector.html)
> 
> Something along these lines?

Yeah haha. I abandoned that idea now and went with something simple and split my variables into classes. Nice and simple. Useful link though.

---

<div class="post-metadata">

### Author: ![inghy](https://avatars.discourse-cdn.com/v4/letter/i/e0b2c6/32.png) [@inghy](https://discussions.unity.com/u/inghy)
#### Post date: [April 3, 2023, 3:34pm UTC](https://discussions.unity.com/t/hide-variable-depending-on-bool/663368/4 "2023-04-03T15:34:10Z")

</div>

Something like this could work, if you happen to have this need again:

```csharp
#if UNITY_EDITOR

[CustomEditor(typeof(CameraMovement))]
public class CameraMovementEditor : Editor
{
    private CameraMovement _cam;
    private SerializedProperty _checkField;
    private SerializedProperty[] _lockedFields;

    private void OnEnable()
    {
        _cam = (CameraMovement) target;

        //nameof is used to facilitate refactoring (must be either the fields public or this class inside CameraMovement)
        _checkField = serializedObject.FindProperty(nameof(_cam.edgePan));

        _lockedFields = new[]
        {
            serializedObject.FindProperty(nameof(_cam.distFromEdge)),
            serializedObject.FindProperty(nameof(_cam.panSpeed))
        };
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        GUI.enabled = true;
        //the bool field has to be shown
        EditorGUILayout.PropertyField(_checkField);

        GUI.enabled = _checkField.boolValue; // if you want to see them but have them locked

        foreach (var field in _lockedFields)
        {
            // if (_checkField.boolValue) //if you want to hide them completely
                EditorGUILayout.PropertyField(field);
        }

        GUI.enabled = true;

        serializedObject.ApplyModifiedProperties();
    }
}
#endif

```
