List<> variable content different in game. I know, this is confusing, please see explanation inside

Maybe I’m crazy. See for yourself. I have a List of strings set up like in the first image. Initially it ended with “CardFaces/Card_24”. All works fine. No errors.

Now I’ve added two more cards. saved the c# file. No errors, all works except!!! unity doesn’t see the “CardFaces/Card_25” and “CardFaces/Card_26”.

The foreach loop to check what is inside the List was added after adding the above two strings. And it runs, no errors, except it also shows there is no “CardFaces/Card_25” and “CardFaces/Card_26” in the List.

what the actual ducking duck is going on?
8753110--1186087--01.png
8753110--1186090--02.png
8753110--1186093--03.png

See here. If the component is saved in a scene or prefab, the list will be serialized (so if you edit the list’s contents in the inspector, they’ll still be there after a save and load).

It doesn’t matter what the field initializer does; Unity will clobber it with the saved values.

If you don’t mind not seeing the list in the editor, you can use the System.NonSerialized attribute to tell Unity to not serialize the field.

I see, doing some test and indeed if the list appears in the unity interface, on the game object attribute window (or how is called) then it will ignore any chances done after attaching the script to the gameobject. choosing reset shows the correct values.

But I never changed those values inside the game object property window.

Is this some new bug or was always like this? I don’t recall meeting this type of behavior before.

I guess people complains that values set up in the editor will vanish and now unity is enforcing this?

As far as I know, this is how the editor has always worked.

You might be thinking about how editing things on a prefab results in a bolded override, and how changing a non-overridden value on the prefab causes them to change on prefab instances, too.

But this is to be expected: all of that data is serialized!

Field initializers execute during construction of the object. Unity sees those values and says “right, I’ll serialize those.” Editing a field initializer does not change anything in the eyes of Unity.

:eyes: