private IEnumerator ServeCard() { //wait until all card GameObject are instantiated yield return(new WaitWhile(() => SpawnController.Instance.cardToSpawn.Count < 52)); //serve progressively the card on the table for (int i = 0; i < bottomStacks.Count; i++) { for (int j = 0; j < i + 1; j++) { //set the y and z offset for the current card to serve on table float currentYLocalOffset = yLocalOffset * j; float currentZLocalOffset = zLocalOffset * (j + 1); //get the current card to serve on table CardController cardController = SpawnController.Instance.cardToSpawn[j]; GameObject cardGameObject = cardController.gameObject; //Remove served card form deck SpawnController.Instance.cardToSpawn.Remove(cardController); //set the new parent for the current card to serve on table cardGameObject.transform.SetParent(bottomStacks[i].transform); //calculate the offset in world coordinates Vector3 offsetVector = bottomStacks[i].transform.TransformVector(0, currentYLocalOffset, currentZLocalOffset); //set the new position of the card Vector3 newPosition = bottomStacks[i].transform.position - offsetVector; //set canvas sorting order of cardController.SetOverrideCanvasSortingOrder((int)currentZLocalOffset, false); //coroutine to move gradually card to new position cardController.MoveToPosition(newPosition); //set flag to make card discovered if is last of the list if (j == i) { cardController.SetIsCovered(false); cardController.SetIsMovable(true); bottomStacks[i].lastCardController = cardController; } yield return(new WaitForSeconds(waitTimeForServe)); cardController.CheckCovered(); } } }