How do I get a variable from every item in a list

Bare with me as this is my first post. I’ll apologize ahead of time if this is not following the proper etiquette of unity answers. I just want to be as clear as possible. I’m not brand new to C# coding but I’m by no means an expert.


I’m working on an RPG and I have 3 scripts I need to work together. The first script I have is a master script (Base_Hero) that contains the stats and variables for the playable characters. The second script I have is designed to inherit all the variables from Base_Hero while allowing me to include character specific mechanics (Base_Adam). An example of this reasoning would be if later down the line I wanted a specific character to be able to double jump. The third script I have is Base_Spell which simply holds values such as mana cost and action point cost.


What I have right now is 2 game object lists in Base_Hero. One called everyAvailableSpell and one called spellLoadOut. everyAvailableSpell is supposed to contain all the spells and abilities the character currently has available to them at their current level. spellLoadOut is supposed to contain all the spells that the player can actively use during combat. A character gains spell slots through level up and each spell has a spell slot cost. So the idea is that you would use a UI to drag spells from everyAvailableSpell list to spellLoadOut similar to Kingdom Hearts and it would allow more customizing to a character.


The code below is written in Base_Adam. My problem is that I’m trying to get the script to comb through each game object contained in the spellLoadOut list, get a reference to the Base_Spell script attached to a gameObject, get the spell slot value located in Base_Spell, and add it to the spellSlots_curr located in Base_Adam. This way I can make sure the current spell slots value doesn’t go past the max spell slots value.


The way I thought I could accomplish this was by having an if statement that compares the current spell slot value to the max spell slot value and proceeds if the current value less than the max. However, it only checks the first item in the list, adds it in and then it just stops.
The next if statement I have checks if the current value is higher than the max value and is supposed to remove the last item in the list and continue doing this until the current spell slot value is less than or equal to the max spell slot value.
The debug message “Too many spells, not enough spell slots” never fires even if I raise a spell’s spell cost value to a higher number. It only adds the value of the first item and then stops.


Here’s the code I’m trying to get to work:

public void SetUpSpellLoadOut()
{
    spellSlots_max = (level + 9) / 10;

    spellSlots_curr = 0;

    if (spellSlots_curr < spellSlots_max)
    {
        for (int i = 0; i < spellLoadOut.Count; i++) 
        {
            Debug.Log("Spell Added");
            Base_Spell spellHolder = spellLoadOut*.GetComponent<Base_Spell>();*

spellSlots_curr += spellHolder.cost_actionPoints;
if (spellSlots_curr >= spellSlots_max)
{
break;
}
}
}
if (spellSlots_curr > spellSlots_max)
{
for (int i = spellLoadOut.Count; i > spellLoadOut.Count; i–)
{
Debug.Log(“Too many spells, not enough spell slots”);
Base_Spell spellHolder = spellLoadOut*.GetComponent<Base_Spell>();
spellSlots_curr -= spellHolder.cost_actionPoints;
spellLoadOut.RemoveAt(spellLoadOut.Count - 1);
if (spellSlots_curr <= spellSlots_max)
_
{_
break;
_
}_
_
}_
_
}_
_
}*_
Edit: Worth noting, this event is called during the Start() function of Base_Adam

@sath got the right answer. My for loop was written wrong.