Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CherryPicker · Sep 27, 2018 at 01:07 PM · editor-scriptingserializationserializedpropertyserializeeditorscript

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.

alt text

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 ?

falsetrue.png (7.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dan_wipf · Sep 27, 2018 at 05:49 PM 0
Share

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?

avatar image Bunny83 dan_wipf · Sep 27, 2018 at 11:20 PM 0
Share

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)

avatar image CherryPicker Bunny83 · Oct 01, 2018 at 09:58 AM 0
Share

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 ?

Show more comments
avatar image
0

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);
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CherryPicker · Sep 27, 2018 at 02:17 PM 0
Share

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.

avatar image dan_wipf · Sep 27, 2018 at 02:55 PM 0
Share

debug the value in editor script.. and see if it’s diffrent

avatar image CherryPicker dan_wipf · Sep 27, 2018 at 03:30 PM 0
Share

It works fine in the editor script. That's weird.

avatar image dan_wipf CherryPicker · Sep 27, 2018 at 04:02 PM 0
Share

can you post the whole part of void Update()?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to modify a Gradient SerializedProperty 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges