How to properly serialize a variable in a custom inspector ? getting a weird Debug result [UnityEditor][Serialize]

I was working on a custom inspector. I managed to make it look as I wanted. It also seemed to work as supposed.

But I learned that what I was doing was not totally correct, and recommended to use Serialize.

I tried to, but ended up with a weird result. My Debug for a boolean returns both true and false simultaneously no matter what I set in the inspector, it still seems to work normally.


The Debug is placed in the Update() of my target script.

LineScript → Update() → Debug.Log(widthFirst);

Here are the relevant code bits

public class LineScript : MonoBehaviour
             public bool widthFirst;
             Update(){ Debug.Log(widthFirst); }

And the editor

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class LineScriptCustomEditor : Editor
    private LineScript myTarget;
    private SerializedObject soTarget;
    private SerializedProperty widthFirst;

     private void OnEnable()
        myTarget = (LineScript)target;
        soTarget = new SerializedObject(target);
        widthFirst = soTarget.FindProperty("widthFirst");

      public override void OnInspectorGUI()
         if (EditorGUI.EndChangeCheck())

Why is the Debug giving me both true and false at the same time ?

How to properly implement the serialization of the widthFirst variable ?

this is how it works for me with a bolean:


public bool myBool;

void Update() {

if (myBool){
// do stuff

// Editor Script

SerializedProperty myProperty;

void OnEnable() {
myProperty = serialiezedObject.FindProperty(“myBool”);

public override void OnInspectorGUI() {

You need to learn how to debug properly.


Debug.Log("widthFirst: " + widthFirst + " of object" + + " (frame: " + Time.frameCount + ")", gameObject); 

Note the additional “gameObject” parameter at the end. This has the following advantages:

  • First it uniquely identifies your debug log. Just logging a value is never a good idea since you don’t know to which log statement a certain log entry belongs to. If you log two different boolean values somewhere you have no idea which log belongs to which.
  • Adding the object name helps to identify cases where you might have the same script on several objects.
  • Printing the current frame (the time would work as well) helps to distinguish when a log happens chronologically. If two logs happens at the same frame they most likely belong to two different log statements or you did something fundamentally wrong, like calling Update manually somewhere.
  • Finally passing an object as optional parameter to the Log method will register that object as context object for this log entry. If you single click the log in the console, that object will be “pinged” in the hierarchy so it makes it easier to identify the object that produced that log.

ps: You don’t need to create your own SerializedObject instance. A custom editor already has the [serializedObject][1] property. It already represents the selected object(s) that this editor edits.

Finally if you have trouble with debug log statements, make sure you disable “collapse” in the console. It will automatically group “the same” log messages into one entry and just increase the number at the end to indicate how many times this message has been logged. If you use the log statement i’ve mentioned this might not be an issue anymore since the frameCount makes the message different each frame.
[1]: Unity - Scripting API: Editor.serializedObject