- Home /
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:
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 :).
Answer by troien · Nov 08, 2014 at 07:39 PM
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 :p
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.
well thanks for your answer, that actually helped me understand things a little bit more, i solved the problem by removing :
DrawDefaultInspector, which i didn't realized i deleted unwillingly while copying the code. Sorry for the mess :).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Creating Objects and Hiding when out of Range 1 Answer
Create custom menu script in inspector, like 'Canvas Scaler (Script)' 1 Answer
How can I create a list of preset class/objects to be used in the editor 1 Answer
How would I go about creating a custom Unity Event in a Custom Unity Editor/Inspector? 0 Answers