Editor Script + Struct Help

Hi guys, I’m working on an editor script.
I have the first script where I have a struct called Speed, and a List of them.
In the editor script I have a button that add a Struct of type Speed in that list and all works fine… But when I close unity or I do something, this List seems to be cleared, so all any other Speed I 've added isn’t there anymore…
Any help about that?

You need to add this to your struct:

And maybe this to your Field (when its private):

Tried with it but doesn’t work…

Could you show the code of your editor script and component/struct?

I’m pretty sure that custom structs cannot be serialized by Unity ( which kind of sucks :frowning: ).
You can work around this by using classes that extend Mono’s Object class, but it’s not ideal since they wouldn’t pass by value anymore.

Here’s the code:

Component Script

public List<Speed> customSpeeds = new List<Speed>();

[System.Serializable]
    public struct Speed
	{
		public string name;
		public float forward;
		public float backward;
		public float side;
		
		public Speed(string _name,float _forward,float _backward,float _side) 
		{	
			name = _name;
			forward = _forward;
			backward = _backward;
			side = _side;
		}
		
		
	}

Editor Script:

if(GUILayout.Button("New Speed")  speedName != string.Empty)
				{
					 Motor.Speed toAdd = new Motor.Speed(speedName,10,6,8);
					 motor.customSpeeds.Add(toAdd);
					 EditorApplication.SaveAssets();
					
				}

It adds well the Speed struct to customSpeeds List, but when I close Unity it’s cleared :expressionless:

I tried to copy paste the gameObject where is the script, and it do the same thing, clears the array… :\

Did you thry what Aniani suggested? Changing your struct to a class.

Yes, I tried it now and work :slight_smile: Thanks both for the help… changing the struct to a class worked well :slight_smile: