Search Unity

List<> issue

Discussion in 'Scripting' started by a_user_that_codes, Nov 21, 2017.

  1. a_user_that_codes

    a_user_that_codes

    Joined:
    Feb 23, 2017
    Posts:
    6
    Hello! I am having an issue with lists that I don't understand.

    Here is what happens...

    I make a public list like so:

    public List<Color> colors;
    (I have also tried public List<Color> colors = new List<Color>();)

    Then in a method I make a new list like so:

    void ExMethod() {
    List<Color> colorList = colors;
    }

    In that method I remove colors from the new List I created in the method and return it.

    My goal: I want to make a new list of colors with me being able to remove colors without removing anything from the reference list (colors) so that I can use my new list without messing up the reference list so I can use it over and over.

    Since it's public I add colors in the inspector and when I run the game the colors are removed from the public list which is not what I want. I found out that if I just add the colors I want in the code to the new list like:

    colorList.Add(Color);

    It works just fine but then i would have a long method filled with colorList.Add(Color). I don't want that. What I can't get my head around is I remove colors from colorList not colors yet it removes it from colors..... any ideas on how I can fix this? I have been programming for a while and I can tell this is not the best way of doing things because of all the list I create but I am just looking for a answer to why it removes colors from the list I am not even removing it from.. Thanks
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    These are reference types. When you create colorList and assign it colors, you are not making copies of the lists. You're making two different names that reference the same exact list.

    If you actually want to make a copy of the list that is independent of the original, you can do it two ways. First is what's called a "shallow copy" where you have two independent lists that you can add and remove from separately, but their contents can still reference the same objects inside. The second is what is called a "deep copy" where you actually clone everything in the first list and can make changes to those contents and nothing is ever seen different on any of the contents of the first list (basically copying all the contents one at a time inside the list).

    There's a lot of discussion on the best way of doing both of those in C#, so I'll leave that for you to research what is best for you. My guess though is you're looking for a shallow copy of the list. Here is just one simple way of doing that:

    Code (csharp):
    1.  
    2. List<Color> colorList = new List<Color>();
    3. foreach (Color c in colors)
    4. {
    5.     colorList.Add(c);
    6. }
    7.  
    So if you do the above you will have two different lists (adding a new Color to colorList will not add it to colors, and removing one from colorList will not remove it from colors), but if you make changes to specific colors stored in both lists they will both see the changes to those colors.
     
  3. a_user_that_codes

    a_user_that_codes

    Joined:
    Feb 23, 2017
    Posts:
    6
    Hey thanks! I have not tested it yet but I know it is what I want. I forgot I can use foreach to add to other things like list arraylist etc. Simple mistake on my end. And you taught me a little more about list. Thanks!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Good response, just wanted to add that when you're talking about value types, you can make a new list like so:
    Code (csharp):
    1.  List<Color> onelist = new List<Color>(); // plus filled in of course.
    2. List<Color> otherlist = new List<Color>(onelist);
    3.  
     
    TaleOf4Gamers likes this.