- Home /
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;
[CustomEditor(typeof(LineScript))]
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()
{
soTarget.Update();
EditorGUI.BeginChangeCheck();
...
EditorGUILayout.PropertyField(widthFirst);
...
if (EditorGUI.EndChangeCheck())
{
soTarget.ApplyModifiedProperties();
}
}
}
Why is the Debug giving me both true and false at the same time ?
How to properly implement the serialization of the widthFirst variable ?
Answer by Bunny83 · Sep 27, 2018 at 05:23 PM
You need to learn how to debug properly.
Try:
Debug.Log("widthFirst: " + widthFirst + " of object" + gameObject.name + " (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]: https://docs.unity3d.com/ScriptReference/Editor-serializedObject.html
i know you know unity well, but if you debug just the bool like he did,
Debug.Log(widthFirst);
it should say(true or false) or am i wrong?
He said
$$anonymous$$y Debug for a boolean returns both true and false simultaneously
That is not possible. One Debug log call creates exactly one log entry. If he gets 2 he either has several objects with the same script or has collapse enabled so he only sees the two states as two entries which never change (besides the count at the end)
Just wanted to reply that you were correct. I was collapsing and the script was attached to multiple objects which was causing the issue. So that issue is fixed.
With that being said. I want to make sure that the widthFirst bool value persists. You say that the object is serialized by default if it's an editor. Should I keep the SerializedProperty of widthFirst even if it is public ? Also, since soTarget is serialized by default I assume I don't need the soTarget.Update() or BeginCheck() and EndCheck() either ?
Answer by dan_wipf · Sep 27, 2018 at 01:38 PM
this is how it works for me with a bolean:
//monobehaviour
public bool myBool;
void Update() {
if (myBool){
// do stuff
}
// Editor Script
SerializedProperty myProperty;
void OnEnable() {
myProperty = serialiezedObject.FindProperty(“myBool”);
}
public override void OnInspectorGUI() {
EditorGUILayout.Propertyfield(myProperty);
I still get the same weird Debug result (both true and false at the same time).
This version is very similar to $$anonymous$$e, the only 2 differences I can see are the fact that
1) you use if(myBool){...} in the update, while I use the bool in a method that is called by another method from update.
2) you use
void OnEnable() {
myProperty = serialiezedObject.FindProperty(“myBool”);
}
ins$$anonymous$$d of
private SerializedObject soTarget;
private SerializedProperty widthFirst;
private void OnEnable()
{
soTarget = new SerializedObject(target);
widthFirst = soTarget.FindProperty("widthFirst");
}
$$anonymous$$aybe your way and maybe $$anonymous$$e are actually correct, but how do you explain the debug giving me both true and false at the same time ?
I am still not sure of this thing.
debug the value in editor script.. and see if it’s diffrent
It works fine in the editor script. That's weird.
can you post the whole part of void Update()?
Your answer
![](https://koobas.hobune.stream/wayback/20220612175348im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Dynamic serialized fields based on enum 0 Answers
How can I make dynamic gui based off of the variables from a serializable class? 1 Answer
SerializedProperty with children class fields 2 Answers
What would be the best way to go about loading and saving a Serializable object in JSON with Unity? 0 Answers