How to get a Value of a Variable stored in a List?

So I was able to FINALLY get the answer I was looking for:
Power = techList[0].Power;
That worked, but of course its only temporary as this sets Power to be whatever Element zero is and of course enemies and party members will have more than 1 skill. Dont have to deal with it yet but how would I go about making it more global?


DamageFormula is not working because it is not finding Power variable for any Skills stored in a List of scriptableObjects. If I make a public reference to BaseTech and drag skill into it, formula follows through. How to access variables stored in a list?

Base Creature.

[CreateAssetMenu(fileName = "New Creature", menuName = "RPG/Creature")]
public class BaseCreature : ScriptableObject
{
    public string Name;
    public int MaxHP;
    public int HP;
    public int Strength;
    public int Defense;
    public List<BaseTech>List;
}

BaseTech.

[CreateAssetMenu(fileName ="New Technique", menuName ="RPG/Tech")]
public class BaseTech : ScriptableObject
{
    public string Name;
    public int Power;
}

Creature

public class Creature : MonoBehaviour
{
    public BaseCreature baseCreature;
    public BaseTech baseTech;
    private Technique technique;

    public string Name;
    public int MaxHP;
    public int HP;
    public int Strength;
    public int Defense;
    public int Damage;
    public int Power;
    public List<BaseTech>techList;

    private void Awake()
    {
        Name = baseCreature.Name;
        MaxHP = baseCreature.MaxHP;
        HP = baseCreature.HP;
        Strength = baseCreature.Strength;
        Defense = baseCreature.Defense;
        techList = baseCreature.List;
        Power = baseTech.Power;
    }

    private void Start()
    {
        DamageFormula(Damage);
    }

    // Equivalent to Hurt//
    public void DamageFormula(int amount)
    {
        Damage = Mathf.RoundToInt(Strength + Power - Defense * Random.Range(1.00f, 1.125f));
        HP = Mathf.Max(HP - Damage, 0);
        if ( HP == 0)
        {
            Die();
        }
    }

    public void Die()
    {
        Destroy(this.gameObject);
        Debug.LogFormat("{0} has died!", Name);
    }
}

Technique.

public class Technique : MonoBehaviour
{
    BaseTech baseTech;

    public string Name;
    public int Power;
    private Vector3 targetPosition;
 
 

    private void Start()
    {
        Name = baseTech.Name;
        Power = baseTech.Power;
    }

    //Equivalent to Cast//
    public void Attack(Creature target)
    {
        targetPosition = target.transform.position;
        Debug.Log(Name + " was cast on " + target.name + "!");
    }
}

I’m home now, so if you need to see the code let me know …

Code added.

There are a number of things you can do including iterating them or direct index access. The official documentation shows example usage.

As a suggestion, to avoid unnecessary confusion it is worth keeping variable names distinct from language keywords.

Thank You

But this is the worst thing that you can do to a beginner programmer. I could not find what I was looking for, does not mean its not there, just that I am inexperienced with coding to the point that I don’t recognize it even if i was looking right at it.

Please. Something less daunting.

So I was able to FINALLY get the answer I was looking for:

Power = techList[0].Power;

That worked, but of course its only temporary as this sets Power to be whatever Element zero is and of course enemies and party members will have more than 1 skill. Dont have to deal with it yet but how would I go about making it more global?

Apologies, I did not realise giving a link to the documentation would be so daunting but glad to see you got your answer in the end.

That depends very much on how you want your mechanics to work. For example, is just one skill used in an attack and one skill used by the defender? Or, is the attack some function of all skills possessed by the assailant and likewise for the defence (e.g. a simple sum of all powers)?

If the former then, rather than using zero, you would need some way of identifying the skill that was used. To do that, you could consider using a Dictionary instead of a list. An example use of a Dictionary could look like this :

    public enum eAttackTech
    {
        Sword, Axe, Spear, Gun, Tank, Warplane
    }

    public void AddTech(eAttackTech techType, BaseTech techDetails)
    {
        myTech[techType] = techDetails;
    }

    public int GetPower(eAttackTech tech)
    {
        return myTech[tech].Power;
    }

    // Variables private to restrict access to them.
    private Dictionary<eAttackTech, BaseTech> myTech = new Dictionary<eAttackTech, BaseTech>();

Generally, it is worth familiarising yourself with the data containers available in C#. See here under the heading Choosing a Collection.

As a point of interest, the Creature “has-a” base tech list. If the attack is incoming on them, does this mean the attack has to happen with a tech that the creature itself possesses? What if the assailant does not have any of those tech’s?