Need help accessing a script element in C# please!

alt textHi Unity,

I’m having trouble figuring out how to access a specific script element (seen near the cursor in the picture). Do I need to create a foreach loop to get at this variable? All I want to do is add more health potions via an onTriggerEnter script attached to a box, but I can’t seem to figure out how to access it.

Thanks!

If you now the order of the elements in BottomActionbarSlots won’t change you could access that script directly:

void OnTriggerEnter(Collider other)
{
    if (other.tag == "YourTag")
    {
        ActionBars bars = other.GetComponent<ActionBars>();

        HealthPotion potion = bars.bottomActionbarSlots[1].item as HealthPotion;
        ++potion.YourVariableWithTheNumberOfPotions;
    }
}

A more secure way to get the script would be using Linq. This way it can be whatever element in the list, assuming BottomActionbarSlots is a list and not an array.
For this solution you have to add using System.Linq; in the using declaration of your script.

void OnTriggerEnter(Collider other)
{
    if (other.tag == "YourTag")
    {
        ActionBars bars = other.GetComponent<ActionBars>();

        HealthPotion potion = bars.bottomActionbarSlots.FirstOrDefault(i => i.item is HealthPotion).item as HealthPotion;
        ++potion.YourVariableWithTheNumberOfPotions;
    }
}

I haven’t tried it as I don’t have the rest of the classes but it should work.