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.
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.
This looks like it will work! Your first example answered exactly what I was looking for, just didn't know how to phrase it in C#. (The bottomActionBarSlots[1] bit that is). Thanks!!
This looks like it will work! Your first example answered exactly what I was looking for, just didn't know how to phrase it in C#. (The bottomActionBarSlots[1] bit that is). Thanks!!
– AddyarbGlad to help! Mark it as answered if you don't mind.
– dmg0600