C# - Two lists linked with the same value
In script A, I have a generic list:
public List<int> firstList = new List<int>();
In script B, I have a second list:
public List<int> secondList = new List<int>();
I then do this:
scriptB.secondList = firstList;
Whenever I change firstList, secondList gets changed too, and vice versa. I don't want this to happen. What am I missing?
Answer by YoucefB · Oct 01, 2017 at 07:43 PM
You have to make a new list, but you are referencing an existing one
Try:
scriptB.secondList = new List<int>(firstList);
Thank you! Turns out I'm a C# newbie :) I'm used to other languages where assignment is by value.
Your answer
Follow this Question
Related Questions
Is it possible to use . Operator to access variables in classes in lists 1 Answer
List returns NullReferenceException, but when it's visible in the Inspector it works perfectly ??? 1 Answer
How do I load a scene when my List is empty? 0 Answers
Updating UI Text of an existing element in a list. 0 Answers
Removing item from list makes it appear at the bottom ? 1 Answer