Optimize code snippet

Hello, I have this code snippet that is used everywhere in my code and I would like to optimize it better as it is used always during time critical parts.

       Transform tempStatusGroup;
        tempStatusGroup = targetHpBar.transform.GetChild(0);
        bool checkIfAdd = true;
        int totalStatusEffects = 0;

        //Now looping through each buff / debuff!! Which is contained in the monsters HP bar as a child of status group!
        foreach(Transform child in tempStatusGroup){
            //Ignore DoT Effects and HoT Effects!
            if (child.name.Contains(status) && !child.name.Contains("dot") && !child.name.Contains("hot")){
                string childText = child.GetComponentInChildren<Text>().text;
                int childTextInt = IntParseFast(childText);

                if (childTextInt < turnsOfStatus){
                    child.GetComponentInChildren<Text>().text = turnsOfStatus.ToString();
                }
                checkIfAdd = false;
            }

            //Count the total amount of status effects and make sure it is not above 10!
            totalStatusEffects++;
            if (totalStatusEffects >= 10){
                //Too many status effects on monster! DON'T ADD MORE!
                checkIfAdd = false;

Pretty much I have a monster with some gameobjects (with only a text component) to display their buffs/debuffs, and I need to check them to look for certain one’s quickly and efficiently. They also have a text component with a number and I always negative that number by one to count down, and its a lot of steps to make it work and I don’t like that.

Whenever I check for something like Damage over time debuff, I have to search through the children to see if the name matches the string and I know this is not the best way to do it so any help on that would be greatly appreciated.

Thanks for any advice!

Use an enum with a list of the various statuses that are capable of being “contracted”, like so:

public enum StatusEffects
{
    Normal = 0,
    Poisoned,
    Snared,
    Disliked
}

You can then refer to these statuses in a much more readable way in your code without actually using strings- the values are simply ints in this case, with a sort of shell. Because they’re ints, you can check the values very quickly. For each character, you can use a List() to store the current statuses, or you can look up/look into turning an enum into “flags” and perform bitwise operations to very very quickly check which, if any, statuses exist on the character. The latter option is by and far the fastest, hands down, but it’s also a little hard to read, so no one will blame you for using the List method instead.

I think you should make the status effects into classes and have them responsible for applying their own effects. When a status effect is applied to a monster, it can be added to a list. Whenever your monster decides to check its buffs/debuffs it can send a signal to each class in the list to apply its own effect.

^ to clarify, I think in that case you would create a base “StatusEffect” class and then inherit that to create sub-classes, so they could all be stored in one list as “List” and have some common functions that could be called without knowing their specific types. eg:

public abstract class StatusEffect
{
    public abstract void ApplyEffect();
}

public class PoisonEffect : StatusEffect
{
    public override void ApplyEffect()
    {
        //DO STUFF
    }
}

or use interfaces to do the same thing, but this way you can have some common implementation as well, if you want. shrugs

Nice, thanks guys! I will for sure be looking into doing it one of these ways, thanks for the advice and the right direction!