Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Editor & General Support' started by altepTest, Jan 24, 2023.

  1. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,170
    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?
    01.png
    02.png
    03.png
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    726
    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.
     
  3. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,170
    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?
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    726
    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.
     
  5. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,170