Search Unity

C# Incrementing variable once instead of repeatedly while enemies are in a certain Enum state.

Discussion in 'Scripting' started by Masterredlime, Sep 21, 2017.

  1. Masterredlime

    Masterredlime

    Joined:
    Sep 11, 2017
    Posts:
    12
    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
    Code (CSharp):
    1. public class EnemyStateCounter : MonoBehaviour {
    2.  
    3.     public int enemiesEngaging;
    4.     public int enemiesIdling;
    5.  
    6.     void Start () {
    7.  
    8.         // Clamp int from going below 0
    9.         enemiesEngaging = Mathf.Clamp(0, 0, 2);
    10.         enemiesIdling = Mathf.Clamp(0, 0, 100);  
    11.     }
    12.    
    13.  
    14.     void Update () {
    15.  
    16.      
    17.    
    18.     }
    19. }
    20.  
    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.

    Code (CSharp):
    1.     public void RobotStateHandler()
    2.     {
    3.         if (state == RobotState.IDLING)
    4.         {
    5.             minTargetDistance = 10f;
    6.         }
    7.  
    8.  
    9.         if (state == RobotState.ENGAGING)
    10.         {
    11.             minTargetDistance = 2f;
    12.         }
    13.  
    14.         if (state == RobotState.ENGAGING)
    15.         {
    16.             stateCounterScript.enemiesEngaging++;
    17.         }
    18.  
    19.         //foreach (int i in System.Enum.GetValues(typeof(RobotState)))
    20.         //{
    21.         //    // Add some way to update the enemiesEngaging int after each state change as opposed to constantly adding when in the state
    22.          
    23.  
    24.         //}
    25.  
    26.         //for (int i = 0; i < 2; i++)
    27.         //{
    28.  
    29.         //}
    30.      
    31.     }
    So is there anyway that I can create an increment or reduction just once every time my enemy changes state?
     
  2. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    You could have a state called ENTER_ENGAGING.
    Increment your variable there and move your enemy to ENGAGING after that.
    Just an idea....
     
  3. Masterredlime

    Masterredlime

    Joined:
    Sep 11, 2017
    Posts:
    12
    @DeathQuake That would mean having the enemy stay in the Enter_Engaging state short enough that it only increments the enemiesEngaging integer once before transitioning to the ENGAGING state. Any suggestions as to how I can do that?
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Create a "SetState" function of some kind, or an OnStateChanged event callback, where you can perform one-time actions at the time you change the state based on the current state and new state. Use that function/event for all your state changes.