I'm creating a guitar tab program.
You select notes (GameObject), type a chord name (string), then press a button to add the "Chord" to a list.
The Chord class is just a String and a list of GameObjects. I use currentChord to hold the current selection/name.
When I select a note, I add it to currentChord.selectedList.
When I type a name, I make it currentChord.name.
Chord currentChord;
List<Chord> allChords;
When I click a button, currentChord gets added to allChords (allChords.Add(currentChord)).
The problem is that it's instanced. So when I click to add a different selection/name, the selection of everything in the allChords.notes list changes...
Do I have to use the "new" keyword?
Yes, you have to use the new
keyword.
You are adding the same instance to the list over and over, so you end up with a list of references to the same instance.
Create a new instance from the data in currentChord
to add to the list, or add the instance in currentChord
to the list and then create a new instance and assign to currentChord
.