Search Unity

Editor Script + Struct Help

Discussion in 'Scripting' started by 95KillerZ95, Feb 10, 2012.

  1. 95KillerZ95

    95KillerZ95

    Joined:
    May 27, 2011
    Posts:
    253
    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?
     
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
  3. 95KillerZ95

    95KillerZ95

    Joined:
    May 27, 2011
    Posts:
    253
    Tried with it but doesn't work....
     
  4. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Could you show the code of your editor script and component/struct?
     
    Last edited: Feb 10, 2012
  5. Aniani

    Aniani

    Joined:
    Mar 17, 2011
    Posts:
    31
    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.
     
  6. 95KillerZ95

    95KillerZ95

    Joined:
    May 27, 2011
    Posts:
    253
    Here's the code:

    Component Script

    Code (csharp):
    1.  
    2.  
    3. public List<Speed> customSpeeds = new List<Speed>();
    4.  
    5. [System.Serializable]
    6.     public struct Speed
    7.     {
    8.         public string name;
    9.         public float forward;
    10.         public float backward;
    11.         public float side;
    12.        
    13.         public Speed(string _name,float _forward,float _backward,float _side)
    14.         {  
    15.             name = _name;
    16.             forward = _forward;
    17.             backward = _backward;
    18.             side = _side;
    19.         }
    20.        
    21.        
    22.     }
    23.  
    24.  
    Editor Script:

    Code (csharp):
    1.  
    2.  
    3. if(GUILayout.Button("New Speed")  speedName != string.Empty)
    4.                 {
    5.                      Motor.Speed toAdd = new Motor.Speed(speedName,10,6,8);
    6.                      motor.customSpeeds.Add(toAdd);
    7.                      EditorApplication.SaveAssets();
    8.                    
    9.                 }
    10.  
    11.  
    It adds well the Speed struct to customSpeeds List, but when I close Unity it's cleared :|
     
  7. 95KillerZ95

    95KillerZ95

    Joined:
    May 27, 2011
    Posts:
    253
    I tried to copy paste the gameObject where is the script, and it do the same thing, clears the array.. :\
     
  8. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Did you thry what Aniani suggested? Changing your struct to a class.
     
  9. 95KillerZ95

    95KillerZ95

    Joined:
    May 27, 2011
    Posts:
    253
    Yes, I tried it now and work :) Thanks both for the help... changing the struct to a class worked well :)