Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SerializedObject data getting wiped when apply to prefab?

Discussion in 'Scripting' started by dmcdonough, Jun 27, 2012.

  1. dmcdonough

    dmcdonough

    Joined:
    May 18, 2012
    Posts:
    2
    Like so many others I am having terrific frustrations trying to get a custom editor to set data on my objects/prefabs and have it save. After following the guide on this thread, my code looks like this:

    First, the storage container class and the actual custom component:

    Code (csharp):
    1. [System.Serializable()]
    2.     public class CreatureAnimationPair : Object
    3.     {
    4.         public string animationHandle { get; set; }
    5.         public CreaturerAnimationType type { get; set; }
    6.    
    7.         public CreatureAnimationPair (string animationHandle, CreatureAnimationType type)
    8.         {
    9.             this.animationHandle = animationHandle;
    10.             this.type = type;
    11.         }
    12.     }
    13.    
    14.     [System.Serializable()]
    15.     public class CreatureAnimation : MonoBehaviour
    16.     {
    17.         public List<CreatureAnimationPair> animationPairs;
    18.        
    19.         ...
    20.        
    21.     }
    Then, the editor class:

    Code (csharp):
    1.  [CustomEditor(typeof(CreatureAnimation))]
    2.     public class CreatureAnimationEditor : Editor
    3.     {
    4.         SerializedObject creatureAnimationSO;
    5.         CreatureAnimation creatureAnimation;
    6.    
    7.     void OnEnable ()
    8.     {
    9.         creatureAnimationSO = new SerializedObject(target);
    10.         creatureAnimation = (CreatureAnimation)target;
    11.        
    12.         Animation animation = creatureAnimation.gameObject.GetComponentInChildren<Animation>();
    13.         if (animation != null  creatureAnimation.animationPairs.Count <= 0)
    14.         {
    15.             List<CreatureAnimationPair> newPairs = new List<CreatureAnimationPair>();
    16.             foreach (AnimationState anim in animation)
    17.             {
    18.                 newPairs.Add(new CreatureAnimationPair(anim.name, CreatureAnimationType.None));
    19.             }
    20.        
    21.         creatureAnimation.animationPairs = newPairs;
    22.        
    23.         EditorUtility.SetDirty(target);
    24.         EditorUtility.SetDirty(creatureAnimation);
    25.         creatureAnimationSO.ApplyModifiedProperties();
    26.     }
    In this case, CreatureAnimationPair is an enum set I've made. When I use this on a creature instantiated in the scene, it'll go through all its animations like I want it to and fill out the list of pairs. I can see them in the inspector using my custom GUI just fine. But as soon as I apply that to a prefab version of the creature, all the data in that list gets wiped -- all the strings are emptied and all the enum values become garbage. Why is this happening? How do I get the changes I make in the scene to persist into the prefab?