- Home /
The question is answered, right answer was accepted
why a list of transforms is empty after loading new scene?
I have a settings class with a static list filled with Transforms. If I load a new scene and try to get one of the items from the list, all entries are set to NULL.
Is there a reason why Unity can't keep Transforms in a List from one scene to another?
Answer by dubbreak · Jun 11, 2015 at 07:55 PM
Because the Transforms are by reference not by value. When a new scene loads they all end up null (assuming the Transforms are of scene objects).
If you want to keep them around you either need to make copies of the Transforms to put in your list (which not change when the original transform changes) or use Vector3 or some other datatype to store them statically (updating periodically).
If you are familiar with pointers (and not with references), it's essentially your list is a list of pointers to Transforms (that are owned by the object). When that object goes away that memory location is now null (because of the object destructor). Not 100% correct, since it's references, not pointers.. but the key point is you don't have copies of the Transforms, your list just points to the existing ones and when the object with that transform goes away the transform gets nulled.
Answer by Notter · Jun 11, 2015 at 01:08 PM
If you want to keep GameObjects persistent across scene transitions, you need to have a script on them with:
DontDestroyOnLoad();
I'm taking a guess that your script with the list, is not persistent. but you need yo give more information on your script and what exactly are you doing
Problem is, DontDestroyOnLoad(this) keeps the Transforms also in the history but I don't need them in the next scene.
I my case I have a tile map of a museum and all tiles are stored in a list of transforms. Then it is possible to collect items by playing a $$anonymous$$igame in another scene and after this I come back to the museum.
The list of transforms is in a static settings class which is not bound to a gameobject. All infos (int, string etc.) keep alive when I switch the scenes, but only the transforms in the list get deleted. In my case I found a workaround. I don't really need the transforms at the moment, I only needed the x/y positions, so I made a helper class for the positions and store this in a list.
so my question is in general nature, thy transforms in lists get lost.