UnitController.Attack C# (CSharp) Method

Attack() public method

public Attack ( GameObject tar ) : int
tar GameObject
return int
    public int Attack(GameObject tar)
    {
        if(m_active){
            GetComponent<UnitGUIController>().DisableGUI();
            //attacking unit
            if(tar.GetComponent<UnitController>()!= null){
                return GetComponent<AttackController>().AttackUnit(tar);
            }
            //attacking building
            else{
                return GetComponent<AttackController>().AttackBuilding(tar);
            }
        } else {
            Debug.LogWarning("Inactive unit should not be initiating an attack");
            return 0;
        }
    }

Usage Example

    /// <summary>
    /// Check if attacking unit can attack another unit and then attack with attacking unit
    /// </summary>
    /// <param name="attackingUnit">unitcontroller of attacking unit</param>
    /// <param name="defendingUnit">unitcontroller of unit being attacked</param>
    /// <returns>true if attack succeeded, false if defending unit was out of range</returns>
    private bool AttackUnit(UnitController attackingUnit, UnitController defendingUnit)
    {
        Coords enemyUnitCoords = defendingUnit.GetUnitCoords();

        //check if enemy is within range and has path
        if (attackingUnit.CanAttack(enemyUnitCoords))
        {
            attackingUnit.Attack(defendingUnit);

            //check if enemy unit is dead
            if (!defendingUnit.IsAlive())
            {
                //replace this gameobject in grid with null
                grid.SetGridArray(enemyUnitCoords, null);

                //remove from army list - check which army it is in
                if (defendingUnit.CompareTag("Enemy Unit"))
                {
                    enemyArmy.Remove(defendingUnit);
                }
                else
                {
                    playerArmy.Remove(defendingUnit);
                }

                defendingUnit.Die();

                StartCoroutine(CheckForVictor());
            }

            return(true);
        }

        return(false);
    }
All Usage Examples Of UnitController::Attack