c# list getting nulled at runtime

I am using a list to store some simple animation data for an object, and I’m trying to update that list through an EditorGUI. This all seems to work fine until I run the game, when all the data is seemingly nulled. What am I doing wrong:

Object script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public struct animationData{
	public string name;
	public int start;
	public int end;
	public bool loop;
}

public class animatedVoxelObject : MonoBehaviour {
	
	public List<Material> materialPalette = new List<Material>();
	public List<animationData> anims = new List<animationData>();
	
	public int currentFrame = 0;
	public int frames;
	
	public void AddMaterial( Material mat )
	{
		materialPalette.Add( mat );	
	}

	void Start()
	{
		// Start the animation
		InvokeRepeating( "Animate", 0, 0.05f );
	}
	
	void Animate()
	{
		currentFrame++;
		
		if( currentFrame == frames )
		{
			currentFrame = 0;
		}

		BroadcastMessage( "SetFrame", currentFrame, SendMessageOptions.DontRequireReceiver );
	}
}

Editor Script:

using UnityEngine;
using UnityEditor;
using System.Collections;
 
[CustomEditor(typeof(animatedVoxelObject))]

public class VoxelObjectEditor : Editor 
{
	animatedVoxelObject voxelTarget;
	
	public void OnEnable()
	{
		voxelTarget = (animatedVoxelObject)target;
	}
	
	override public void OnInspectorGUI()
	{
		voxelTarget.frames = EditorGUILayout.IntField( "Frames:", voxelTarget.frames );
		
        if( EditorGUILayout.Foldout(true, "Animations") )
		{
			EditorGUILayout.BeginVertical();
				
				int i = 0;
				animationData returnAnim = new animationData();
			
				foreach( animationData currentAnim in voxelTarget.anims )
				{
					returnAnim.name = EditorGUILayout.TextField( "Name", currentAnim.name );			
					EditorGUILayout.BeginHorizontal();
						returnAnim.start = EditorGUILayout.IntField( "Start", currentAnim.start );
						returnAnim.end = EditorGUILayout.IntField( "End", currentAnim.end );
					EditorGUILayout.EndHorizontal();
					EditorGUILayout.Separator();
				
					voxelTarget.anims *= returnAnim;*
  •  			i++;*
    
  •  		}*
    
  •  		EditorGUILayout.BeginHorizontal();*
    
  •  			GUILayout.Button( "Remove Anim" );*
    
  •  			if( GUILayout.Button( "Add Anim" ) )*
    
  •  			{*
    
  •  				animationData newAnim = new animationData();*
    
  •  				newAnim.name = "Test";*
    
  •  				newAnim.start = 0;*
    
  •  				newAnim.end = 15;*
    
  •  				voxelTarget.anims.Add( newAnim );				*
    
  •  			}*
    
  •  		EditorGUILayout.EndHorizontal();*
    
  •  	EditorGUILayout.EndVertical();*
    
  •  }*
    
  • }*
    }

The problem is that structs don’t get serialized. If you need the data to get saved between sessions, you will have to change that to a class, and use ‘System.Serializable’ to force Unity to store it properly.

[System.Serializable]
public class animationData{
    public string name;
    public int start;
    public int end;
    public bool loop;
}

That’s really all there is to it. If data isn’t shown by the default inspector, it’s because it isn’t getting serialized at all, not just because the inspector doesn’t know how to display it.

Thanks Syclamoth, I had heard of such a thing, but I had no idea that was the specfic use. It now works perfectly.