Custom Inspector, duplicates variables ?

Hello everyone, i have a question about custom inspector. I have 2 variables, useFOV (boolean) and desiredFOV (int) in my WeaponScript. What i wanted was desiredFOV variable will be hidden, and if user checks useFOV boolean as true and then desiredFOV will be shown. I achieved this using this inspector script :

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(WeaponScript)), CanEditMultipleObjects]
public class WeaponScriptEditor : Editor

    public override void OnInspectorGUI()

        weaponScript.useFOV = GUILayout.Toggle(weaponScript.useFOV, "Use FOV?");
        if (weaponScript.useFOV)
            weaponScript.desiredFOV = EditorGUILayout.IntSlider("Desired FOV", weaponScript.desiredFOV, 1, 179);


        serializedObject.ApplyModifiedProperties ();

but the problem is, inspector shows 4 variables, two of useFOV and two of desiredFOV. It duplicates variables not override them.See pics below:

alt text
alt text

I can use HideInInspector in weaponScript to hide them but i don’t think it is what it is supposed to be, it is a hacky way. I beleive i did something wrong. Please help, thanks in advance :).

In the code you give, what is weaponScript? The target?

As for your problem, are you calling base.OnInspectorGUI()? Because if you do, then you’ll need the HideInInspector flag. (Since the base simply draws every serialized field with the exception of fields with that flag) If you don’t, then there is no reason for it to be displayed twice actually :stuck_out_tongue:

And on a sidenote. Do your changes to useFov persist if you close Unity and reopen your scene? Because I believe it doesn’t if you don’t use your serializedObject to set it and then call serializedObject.ApplyModifiedProperties(). I might be wrong, but if you have that problem you’ll need to use EditorUtility.SetDirty (using EditorGUI.BeginChangeCheck and EndChangeCheck) instead.