Hi guys, I'm working on an editor script. I have the first script where I have a struct called Speed, and a List<Speed> 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<Speed> 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: http://unity3d.com/support/documentation/ScriptReference/Serializable.html And maybe this to your Field (when its private): http://unity3d.com/support/documentation/ScriptReference/SerializeField.html
I'm pretty sure that custom structs cannot be serialized by Unity ( which kind of sucks ). 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 Code (csharp): 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: Code (csharp): 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 :|
I tried to copy paste the gameObject where is the script, and it do the same thing, clears the array.. :\