Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UnityEvent not fully serialized?

Discussion in 'Scripting' started by Dark-Protocol, Apr 14, 2016.

  1. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Hello, I have a custom class Trigger which is derived from ScriptableObject. In this class there is a UnityEvent property called onPass. I have a component which has an array of Trigger[] and a custom editor for that component.

    In the custom editor I have this code to iterate through the events:

    Code (CSharp):
    1.    
    2. MyComponent user = (MyComponent)target;        
    3. for (int i = 0; i < user.triggers.Length; i++)
    4.             {
    5.                 SerializedObject serializedTrigger = new SerializedObject(user.triggers[i]);
    6.                 serializedTrigger.Update();
    7.                 SerializedProperty onPass = serializedTrigger.FindProperty("onPass");
    8.                 EditorGUILayout.PropertyField(onPass);
    9.                 serializedTrigger.ApplyModifiedProperties();
    10.             }
    This lists the events in the inspector and I can add new listeners to the events. However when I assign parameter values for the call and deselect the object to edit something else and select it again, the assigned parameter is reverted to what it was initially. The interesting part is that the listener object is there and the action is also preserved, only the parameter is not serialized.
    deselect.PNG
    What is the cause of this issue?
     
  2. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    I found out something today. There is a way to serialize the parameter but it is very weird and I'm going to file a bug report about this because it doesn't seem right.
    As described in the above post, if I set a parameter for the event, deselect the object and select it again, the parameter is reset. However, if I set the parameter to something else than the default value, then go to the object field, select another object and then select the original object again (in this case TextMesh) and assign the method call again (in this case DescriptionController.ShowText), the parameter for the call will remain the one that I've set before changing the object. Then if I select another object in the hierarchy and go back to the one with the events, the change in the parameter will remain the one that I set. So if I want to change any parameter for an event, I need to change the listener object too every time.
    Does anybody know anything about this? If no, I guess it's time for a bug report.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    Try to not do the SerializedObject.Update call. I have a vague recollection of an instance where I had a similar problem, and ditching the Update call fixed it.
     
  4. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Hi Baste, I tried this in hopes it would fix the problem but it didn't change anything. I filed a bug report and received a message that they have been able to reproduce the bug and have sent it for resolution with their devs.
    I made a very small sample project that has this bug and I'm uploaded it for anyone who wants to see the problem for themselves: http://www.filedropper.com/eventsserializebug

    To reproduce, open the only scene in the project, select the Events object, add an event and click the "+" to add a new listener. Select either the Main Camera or the Events object, then in the function select for example GameObject.SetActive then change the parameter value (in this case - boolean) to something different than the initial one. Select another object in the hierarchy and select the Events object again. Voila.
     
  5. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    Did anyone ever manage to solve this problem?