Generic list access variables from a class within a class

Hello I am trying set the variables in a generic list that is inside a class from another class.
The code has been working perfectly fine but whenever I try to call the “setAttackInfo” it crashes because I believe it still does not know that lists of attacks were created or not. The problem is the start function that adds the list attacks and after I cant initialize them properly.

What I am trying to do is have a skill that has base stats, and the skill says it can attack twice or three times and then it creates these two or three attacks which have their own base stats. I am having troubles declaring the variables inside all the attacks of the skill.

Please help me fix this problem, or let me know if I am approaching this incorrectly, thanks!

Manager:

monsterSkills[i][m].attack = true;
monsterSkills[i][m].numOfAttacks = 2;
monsterSkills[i][m].setAttackInfo(1);
monsterSkills[i][m].setAttackInfo(2);

MonsterSkills:

public class MonsterSkills : MonoBehaviour {
    public bool attack;
    public int numOfAttacks = 1;

    [Serializable]
    public class NumberOfAttacks {
        public float flatDamage;
        public float damageModifier;
    }

      public List<NumberOfAttacks> attacks = new List<NumberOfAttacks>();

      void Start () {
          for (int n = 0; n < numOfAttacks; n++){
              attacks.Add(new NumberOfAttacks());
          }
       }

       public void setAttackInfo(int whichAttack){

           attacks[whichAttack].flatDamage = 500;
       }


}

Error I always get: ArgumentOutOfRangeException

Picture of inspector:
2174158--143954--attacks.png

You add one NumberOfAttacks to your attacks list. You should definitely thing about that name again, because it makes no sense. However, then you modify the attacks at index 1 and 2. Though both do not exist, as you only created one attack, that is at index 0.

Don’t I add two “attacks”? I attached a screenshot of the inspector, I’m trying to set the element 0 and element 1, I tried to do: monsterSkills*[m].setAttackInfo(0); which is the first element but that did not work either. And I see how the naming can be confusing, I will fix that up!*

If you create the attacks in the inspector, you shouldn’t add anything in Start () and numOfAttacks should not be used as this number may simply be wrong and it is not too complicated to get the actual number via the actual list.

At which line exactly are you getting an the exception?

Hey thanks for the help, always seems like posting the problem and the answer comes to me shortly after lol. I was thinking too procedurally and should have been thinking more object oriented, I created a constructor inside the NumberOfAttacks class and now I can just do:

monsterSkills*[m].setAttackInfo(100);*
```csharp
*public AttackInfo(float damage){
flatDamage = damage;
}

public void SetAttackInfo(float flatDamage){

    attacks.Add(new AttackInfo(flatDamage));
}*

```

Changed naming convention from NumberOfAttacks to AttackInfo

Works all good now! :slight_smile:

1 Like