Changing Variable From OnTriggerenter

Hey I just wanted some clarification on this problem

I have a script attached to an instatiated prefab enemy. I am trying to change a variable once the trigger is triggered, lets say

public int maxEnemyAttack;

void Start()
{
   maxEnemyAttack = 0;
}

private void OnTriggerEnter(Collider other)
{
   maxEnemyAttack++;
}

But the maxEnemyAttack++ only seems to effect the current enemy and when the next enemy enters, the value will start at zero again, can someone explain what is happening? And how to solve this issue?

Thank you

The code behaves exactly with the programming logic. You create a prefab with a script, you create a copy of it. Each copy has its own maxEnemyAttack.

The simplest solution to do this.

//public int maxEnemyAttack;
public static int maxEnemyAttack;

The longer solution is to create a separate EnemyManager class that holds the counter and adds more enemies by the Instantiate. This second class doesn’t need to have this static value.

private void OnTriggerEnter(Collider other)
{
   enemyManager.AddEnemy();
}

Thanks I will try these later on. I did think of having another script to handle the variable but seemed like overkill

Thank you