x


List or Array of Custom Classes not saving into prefabs properly?

I have a class defined inside a component. It's more or less just a struct of raw data:, i.e.

[ExecuteInEditMode]
class ListOfData : MonoBehavior 
{
    public class DataElement
    {
        public float stuff;
        public GameObject thing;
        //etc.
    }

    List<DataElement> mListOfDatas;



    void Update()
    {



        if (!Application.isPlaying)
        {
             mListOfDatas = mListOfDatas ?? new List<DataElement>();
        }
    }

}

I also have an editor script, changing the contents of mListOfDatas. Unfortunately, this data is not showing up in the component UI, and i'm guessing that only stuff shown in the UI will actually save. Is this the case, and is there a way to force this data to save to the prefab, or force it to display?

I think I'm getting to the limits of the prefab system, here.

more ▼

asked Jul 18 '11 at 02:51 PM

Aubrey Hesselgren gravatar image

Aubrey Hesselgren
287 9 11 19

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

First, only public variables or private variables with SerializeField attribute are serialized. Also you can only serialize arrays or lists of serializeable types. You can turn your DataElement class into a serialized type by adding the Serializable attribute

class ListOfData : MonoBehavior 
{
    [System.Serializable]
    public class DataElement
    {
        public float stuff;
        public GameObject thing;
        //etc.
    }
    [SerializeField]
    List<DataElement> mListOfDatas;
more ▼

answered Jul 18 '11 at 02:58 PM

Bunny83 gravatar image

Bunny83
45k 11 48 206

Thanks so much! Worked perfectly!

Jul 18 '11 at 03:15 PM Aubrey Hesselgren

I lied! It only seems to store the first element when I close and re-open. And sometimes forgets normal, "public" stuff. Does using [SerializeField] once mean I have to start using it everywhere in that class?

Jul 20 '11 at 10:36 AM Aubrey Hesselgren
(comments are locked)
10|3000 characters needed characters left

Add [Serializable] before your custom class declaration. Then the public members will be saved/editable like the ones in your MonoBehaviour.

i.e.

[Serializable]
public class DataElement
    {
...
more ▼

answered Jul 18 '11 at 02:59 PM

Molix gravatar image

Molix
4.8k 15 27 66

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1249
x435
x347
x27

asked: Jul 18 '11 at 02:51 PM

Seen: 2042 times

Last Updated: Jul 20 '11 at 10:36 AM