Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    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?