1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Reddit AMA with Adam Myhill on Cinemachine, Thurs July 27 - 10AM PT. More info.
    Dismiss Notice
  6. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  7. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    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(creatureAnimation);
    24.         creatureAnimationSO.ApplyModifiedProperties();
    25.     }
    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?