public void SpawnUnit(GameObject unitToSpawn, Player player, GridTile tile)
        {
            if (tile != null)
            {
                Unit u = unitToSpawn.GetComponent<Unit>();
                if (u != null)
                {
                    if (CheckResources(u.GoldCost, u.FoodCost, u.LumberCost, player))
                    {

                        player.Resources.Gold += -u.GoldCost;
                        player.Resources.Food += -u.FoodCost;
                        player.Resources.Lumber += -u.LumberCost;
                        GameObject objectToGenerate =
                            (GameObject)
                            Instantiate(
                                unitToSpawn,
                                new Vector3(tile.transform.position.x, 1.1f, tile.transform.position.z),
                                Quaternion.identity);

                        objectToGenerate.transform.parent = this._unitsManagerObject.transform;
                        if (player.SpawnDirection == Vector3.down) objectToGenerate.transform.Rotate(new Vector3(0, 180, 0));
                        else if (player.SpawnDirection == Vector3.left) objectToGenerate.transform.Rotate(new Vector3(0, 270, 0));
                        else if (player.SpawnDirection == Vector3.up) objectToGenerate.transform.Rotate(new Vector3(0, 0, 0));
                        else objectToGenerate.transform.Rotate(new Vector3(0, 90, 0));

                        tile.UnitOnThisTile = objectToGenerate;
                        Unit objectUnit = objectToGenerate.GetComponent<Unit>();
                        objectUnit.OnTile = tile;
                        objectUnit.Owner = player;
                        objectUnit.ResetUnit();

                        this._units.Add(objectToGenerate);
                        player.UnitsOwned.Add(objectToGenerate);
                        UiScript.Instance.TodoLabel.text = "";
                        player.Resources.UnitsSpawned++;
                    }
                }
                else Debug.Log("Not enough resources. SpawnUnit()");
            }
            else
                Debug.Log("Tile is null. SpawnUnit()");
        }
        public static void Attack(Unit pAttackingUnit, GridTile pTile)
        {
            if (pAttackingUnit.HasAttacked)
            {
                Debug.Log("Unit has already attacked.");
                return;
            }
            Unit blockingUnit = pTile.UnitOnThisTile.GetComponent<Unit>();
            blockingUnit.CurrentHitpoints += -pAttackingUnit.Damage;
            pAttackingUnit.CurrentHitpoints += -blockingUnit.Damage;
            pAttackingUnit.Attack();
            blockingUnit.Attack();
            UiScript.Instance.PlayAttackSound();
            blockingUnit.AttackedByPlayer = pAttackingUnit.Owner;
            pAttackingUnit.AttackedByPlayer = blockingUnit.Owner;
            blockingUnit.CheckDeath();
            pAttackingUnit.CheckDeath();

            UiScript.Instance.UpdateUi();
        }
        public static void Move(Unit pSelectedUnit, GridTile pTile)
        {
            // if unit has moved, it cannot move again.
            if (pSelectedUnit.HasMoved)
            {
                Debug.Log("Unit has already moved.");
                return;
            }

            pSelectedUnit.OnTile.UnitOnThisTile = null;
            pSelectedUnit.OnTile = null;
            pSelectedUnit.SetWaypoints(
                new[] { new Vector2(pTile.transform.position.x, pTile.transform.position.z) });
            pTile.UnitOnThisTile = pSelectedUnit.gameObject;
            pTile.Owner = Instance.CurrentPlayer;
            pSelectedUnit.OnTile = pTile;
        }