• 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 the_genius · Sep 19, 2017 at 10:51 PM · editoreditor-scriptingserializationevents

UnityEvent Serialization Problem

Hello, I have an issue relating to using UnityEvents in a Custom Inspector.

I've managed to get it into some shorter code.

This is my script:

 using UnityEngine;
 using UnityEngine.Events;
 
 public class Test : MonoBehaviour {
 
     [SerializeField]
     public MyEvent myEvent;
 
     [System.Serializable]
     public class MyEvent : UnityEvent { }
 }

On its own the script works as expected. But when using the Custom Inspector script bellow there is a problem.

 using UnityEngine;
 using UnityEngine.Events;
 using UnityEditor;
 using UnityEditor.SceneManagement;
 
 [CustomEditor(typeof(Test))]
 public class TestEditor : Editor {
 
     public override void OnInspectorGUI() {
         GUILayout.Label("This is a label just to show the Custom Inspector is working.");
         Test myTarget = (Test)target;
         SerializedObject s = new UnityEditor.SerializedObject(myTarget);
         s.Update();
         EditorGUI.BeginChangeCheck();
         EditorGUILayout.PropertyField(s.FindProperty("myEvent"), true);
         s.ApplyModifiedProperties();
         if (EditorGUI.EndChangeCheck())
         {
             EditorUtility.SetDirty(target);
             EditorSceneManager.MarkAllScenesDirty();
         }
     }
 }

The problem is when entering a value for a static parameter into the Editor. For example, I can add a method to call such as 'GameObject > int layer' and enter a value like bellow.

alt text

Then I can click on another GameObject in the scene. When I go back to this script the value gets replaced with 0 (as bellow). I assume it is not being serialized somehow. But the default Unity Editor can serialize it so it must be possible. Is my code wrong?

alt text

Thank you

example1.png (8.8 kB)
example2.png (8.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

1 Reply

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

Answer by the_genius · Sep 19, 2017 at 11:16 PM

After some more googling I got there. It was just a little mistake in my code.

This line:

 SerializedObject s = new UnityEditor.SerializedObject(myTarget);

means I am creating a new SerializedObject and the editor is serializing the UnityEvent to that object. It is not saving it to the actual SerializedObject the editor uses.

So it should be:

 SerializedObject s = serializedObject;

Its been a long day :(

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

100 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

Related Questions

Get UnityEvent reference from SerializedProperty 1 Answer

Editing prefab fields from custom editor that are not automatically serialized 0 Answers

Storing data about an asset only in the editor 2 Answers

How to assign object reference to ObjectField knowing the object's name? 2 Answers

Custom inspector variable resets automatically 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges