- Home /
How to store data in an object in a scene (in the editor)
I made a custom class which I assign to a gameobject, it has a couple of parameters, one of them of type List < List < Transform > >
. Now the problem is that it seems to lose this data as soon as I reload the scene. whereas a List < Transform >
still contains all its data.
also, all parameters are shown in the inspector, including the List < Transform >
, only not the List < List < Transform > >
.
Is this by design? How do I avoid this?
it can depend on what type you're storing in your list. for example, I ran into a problem some time ago where I was attempting to store a List of System.Type. Unity won't serialize System.Type, so I would always lose my stored list.
Another workaround is to create a custom class that stores your information, and have it inherit from ScriptableObject
I actually typed what type of list it was, but apparently anything between gets deleted automatically. I'll fix my post. The type that doesn't get remembered is List < List >
make sure you hit the code button. adding characters like "
ok, I just fixed it, but for some reason it still shows the previous version? edit: fixed again. I added spaces because it still gets removed otherwise, even when within code tags
Answer by zombience · Nov 12, 2013 at 03:34 PM
Newer Answer
I don't believe that Unity will serialize a two dimensional list. You can create a small serializable class that only contains a list of transforms. then you have a list of classes that each contain a list of transforms.
public class ClassA : MonoBehaviour
{
public List<TransformContainer> containers;
void Start ()
{
}
void Update ()
{
}
}
[System.Serializable]
public class TransformContainer
{
public List<Transform> transforms;
}
Original Answer It depends on what datatype your list is storing. You can store classes in your list and have them serialize, or object references. also, you need to make sure the list is public.
If you are storing custom classes, try this:
[System.Serializable]
public class ClassToStore
{
public int ID;
public string name;
}
public class StorageClass : MonoBehaviour
{
public List<ClassToStore> storage;
void Start()
{
// check storage in some way
}
}
code test
public List<List<Transform>> transforms;
works for me.
UnityAnswers stupidly messed up my text, I edited it now so it makes more sence. The problem is it doesn't seem to remmber anything of type List < List >
you need to hit the "code" button in order to put in code, otherwise the webpage will misinterpret your code as HT$$anonymous$$L tags.
check this image:
also, I don't believe Unity can serialize two dimensional lists. You're trying to store a list of lists of transforms. If you really need that data structure, then I would make a very small class that only has a List of transforms. I'll update my answer above