- Home /
How to add a list as a variable within a list in code C#
How should I create a new List SpellEffect within List Spell spells?
public Spell(string Name, int Duration, List<SpellEffect> StatAffected, int ManaCost, string Type, string DurrType){
name = Name;
duration = Duration;
statAffected = StatAffected;
manaCost = ManaCost;
type = Type;
durrType = DurrType;
}
public List<Spell> spells = new List<Spell>(){
{new Spell("Defend", 2, new List<SpellEffect>(new SpellEffect("DEFENSE","*", 0.1f)), 0, "Buff", "Temporary")}
};
Answer by Unitraxx · Nov 28, 2014 at 07:33 PM
You were almost there, only at line 12 of the example script where you call new List
you should not add the members as constructor arguments (as you are doing now between the round brackets), you should do it between curly braces.
new List<Spell>() {
new Spell("Defend", 2, new List<SpellEffect>(){ new SpellEffect(), new SpellEffect()},
new Spell("Attack", 3, new List<SpellEffect>(){ new SpellEffect(), new SpellEffect()},
};
I actually don't recommend doing it this way however. It's better to first make one list, and then adding the other lists using Add()
.
Wow, thanks! I thought I had tried all possible kind of ways ^^ Got no errors now :)
I will probably rework it in that way (like taking the list from an excel sheet or similar and apply that information to a list and then pass the list forward) but for now this is probably better. I like it tidy like this ins$$anonymous$$d of creating the lists outside of this "argument".
Your answer
![](https://koobas.hobune.stream/wayback/20220613172657im_/https://answers.unity.com/themes/thub/images/avi.jpg)