IEnumerator Refresh() { while (true) { foreach (Unit U in AllEnemies) { if ((LocalMap.GetDistance(Player.GridPos, U.GridPos) <= U.unitStats.VisionRange) && U.IsAttacking == false && Player.IsAttacking == false) { Player.IsAttacking = true; U.IsAttacking = true; LocalMap.TurnModeOn = true; LocalMap.PlayerTurn = true; Player.StopMoving = true; UIM.ClearPath(); LocalMap.CurrentTile = LocalMap.FindTile(Player.GridPos.x, Player.GridPos.y); UIM.ChangeOfSelection(); } //It stars the attack phase if (U.IsAttacking && Player.IsAttacking) //it waits until the player moves { if (PassTurnBool) { if (U.GetComponent <AI_Handler>()) { U.GetComponent <AI_Handler>().CurOnTurn = true; } PlayersTurn = false; //Should not change UIM from here but whatever UIM.Top_Right.CurrentIndicatorText.text = "Enemy's Turn"; U.unitStats.ActionPoints = U.unitStats.MaxActionPoints; yield return(new WaitForSeconds(TimeForAITurn)); /* This just doesnt affect anything literally * while(U.IsUnitInMoveAnim) * { * Debug.Log("Bro"); * }*/ //Then it gives AP to the player Player.unitStats.ActionPoints = Player.unitStats.MaxActionPoints; PlayersTurn = true; UIM.Top_Right.CurrentIndicatorText.text = "Player's Turn"; PassTurnBool = false; } } } yield return(new WaitForSeconds(RefreshRate)); } }