So I’m trying to create a state system for my enemies fighting the player. However I’m unable to update the variable counting how many enemies are in an “engaging” state because the increment happens constantly in the update void instead of once every time an enemy enters the engaging state mode.
This is the variable that will count the number of enemies engaging
public class EnemyStateCounter : MonoBehaviour {
public int enemiesEngaging;
public int enemiesIdling;
void Start () {
// Clamp int from going below 0
enemiesEngaging = Mathf.Clamp(0, 0, 2);
enemiesIdling = Mathf.Clamp(0, 0, 100);
}
void Update () {
}
}
And this is the code that is suppose to call for the increment of upon changing into the Engaging state, however it is constantly adding to the engagingEnemies integer giving the wrong number to the actual enemies engaging.
public void RobotStateHandler()
{
if (state == RobotState.IDLING)
{
minTargetDistance = 10f;
}
if (state == RobotState.ENGAGING)
{
minTargetDistance = 2f;
}
if (state == RobotState.ENGAGING)
{
stateCounterScript.enemiesEngaging++;
}
//foreach (int i in System.Enum.GetValues(typeof(RobotState)))
//{
// // Add some way to update the enemiesEngaging int after each state change as opposed to constantly adding when in the state
//}
//for (int i = 0; i < 2; i++)
//{
//}
}
So is there anyway that I can create an increment or reduction just once every time my enemy changes state?