Controlgroups in an rts, does not reselect properly.

I’m trying to do an Real TIme Strategy game for a school project, having gone in a bit over my head i am now stuck at the very basics of the game not fully understanding how to do things in unity. I’ve gotten the selecting multiple aswell as deselecting working. The problem comes when i want to save the selected units into another list (a control group) by pressing A + a number between 1-9 and storing it in a list that corresponds to that number. After doing a few tests it seems like it does not properly save in the list after completing the code in the if statement.

To explain why i think that, even though it could be something else, look at the picture of the console log.When i select 3 units and then press a + 1 the Selectedunits list gets stored in Controlgroups[1] and then outputs to the log Controlgroups[1].count, and it outputs 3 like it should. But when i only press 1 it should tell me the length of the list in controlgroups[1] which should be 3 but is instead 0, then it should deselect all selected units, copy the list from Controlgroup[1] into Selectedunits and select them. This does not happen and i don’t know why. Instead the first time i press 1 it just deselects everything and does not go through any of my debug.log and the second time it says that controlgroups[1] is empty.

Please do not post code as an image.

What exactly is “SelectedUnits”? It’s most likely either a List or an Array. Those are reference types. So when doing:

ControlGroups *= SelectedUnits;*

You actually store the reference to the List in SelectedUnits in your ControlGroups element. So after that line ControlGroups_ and SelectedUnits do both reference the same array or List. Of course changes to that list would affect both “variables” since they both reference the same list.
To actually store the elements of the current selection in a seperate List you could do:
ControlGroups = new List(SelectedUnits);
This actually creates a new list and populates it with the current elements from the SelectedUnits list
Likewise to restore your selection you want to do something like:
SelectedUnits.Clear();
SelectedUnits.AddRange(ControlGroups*);*
If you don’t like to create a new list everytime you reassign your groups you can create those Lists once in the beginning and do the reverse of what i just wrote:
ControlGroups*.Clear();*
ControlGroups*.AddRange(SelectedUnits);*
Of course this requires that all the Lists in the ControlGroups have been created already._