Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug List not updating

Discussion in 'Scripting' started by JimWebs, Sep 26, 2023.

  1. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    44
    I have a List of strings which I'm editing inside the script by simply adding strings to it as I go. This list does not update in Unity. I don't want to edit it through the inspector with serialize, which I see has been suggested for this kind of thing. Elsewhere someone suggested adding
    [System.NonSerialized], which I did but that didn't work either. List still won't update.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Show an example of your code.

    Though something like this:
    Code (CSharp):
    1. public sealed class SomeMonobehaviour : Monobehavior
    2. {
    3.     private List<string> _someStrings = new List<string>()
    4.     {
    5.         "Bob",
    6.         "Martha",
    7.         "Bazza"
    8.     };
    9. }
    Should work, though, ideally, you do this through the inspector. Though we will need more information of what's not working than "still won't update".
     
    JimWebs likes this.
  3. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    44
    Thanks. It was just as you show, a List with strings, like this:
    public List<string> somestrings = new List<string>()
    {
    "Bob",
    "Martha",
    etc. etc
    }
    But after looking at yours, using sealed, I decided to change mine to "static" and now it updates in unity!! :) So it's working for now ...
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    My use of
    sealed
    has nothing to do with it.

    In your example the collection is
    public
    , thus, serialised. It should be
    private
    , and only
    static
    if it needs to be globally accessible.
     
  5. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    44
    What I mean is it was your use of sealed that gave me a clue as to the problem.
     
    spiney199 likes this.