Question by
cmehra96 · Oct 29, 2020 at 06:33 AM ·
c#coroutinescoroutine errors
Coroutine not executing second time
I am trying to create a card game in which i have to swap two cards, i have created a coroutine executed method of it, for first time that method is getting executed perfectly.I know there are multiple question similar to this, but none of them able to fix my issue.
Please tell me what i am wrong here. Following is my code:
public class GameController : MonoBehaviour
{
/// <summary>
/// Method to remove all the touched cards of Player
/// deck if they belong to removable rules.
/// </summary>
/// <param name="player">Current Player</param>
/// <param name="tempLongTouchedList">Deck of all Touched Cards</param>
public void MultiCardDealtDeckSwap(Player player, Deck tempLongTouchedList)
{
if (HandCombination.IsStraight(player, tempLongTouchedList) == true || HandCombination.isThreeOfKind(player, tempLongTouchedList) == true)
{
tempLongTouchedList.SortByRankAsc();
Card temp = DealtDeck.Deal();
int i = 0;
int size = tempLongTouchedList.CardsCount();
while (i < size)
{
Card removeCard = player.RemoveCard(tempLongTouchedList.GetCardByIndex(i));
DiscardedDeck.Add(removeCard);
i++;
StartCoroutine(SingleSwapAnimationFromDealtDeck(PlayerUIMapping.Instance.cardholder[currentPlayerIndex], removeCard,
CardDistributionAnimation.instance.playersPosition[currentPlayerIndex], false));
}
StartCoroutine(SingleSwapAnimationFromDealtDeck(PlayerUIMapping.Instance.cardholder[currentPlayerIndex], null,
CardDistributionAnimation.instance.playersPosition[currentPlayerIndex], true));
player.AddToHand(temp);
tempLongTouchedList.Clear();
if(DealtDeck.CardsCount()==0)
{
Debug.Log("Dealt Deck empty refilling");
RefillDeck();
}
Invoke("LoadClearMethod", Constants.clearMethodDelay);
StartCoroutine(SwitchTurnToNextPlayer(false, Constants.turnPlayerDelay));
}
}
/// <summary>
/// Method to swap card between Dealt Deck
/// and Player touched card
/// </summary>
/// <param name="player"></param>
/// <param name="touchedCard"></param>
public void SingleSwapFromDealtDeck(Player player, Card touchedCard)
{
Debug.Log("Inside Single Swap Dealt Deck Method");
StopCoroutine(SingleSwapAnimationFromDealtDeck(PlayerUIMapping.Instance.cardholder[currentPlayerIndex], touchedCard,
CardDistributionAnimation.instance.playersPosition[currentPlayerIndex], true));
Card temp = DealtDeck.Deal();
Debug.Log("Dealt Deck touched card" + temp.GetCardImageName());
StartCoroutine(SingleSwapAnimationFromDealtDeck(PlayerUIMapping.Instance.cardholder[currentPlayerIndex], touchedCard,
CardDistributionAnimation.instance.playersPosition[currentPlayerIndex], true));
Debug.Log("Card to be swapped " + touchedCard.GetCardImageName());
Card temp1 = player.RemoveCard(touchedCard);
player.AddToHand(temp);
DiscardedDeck.Add(temp1);
this.touchedCard = null;
if (DealtDeck.CardsCount() == 0)
{
Debug.Log("Dealt Deck empty refilling");
RefillDeck();
}
Invoke("LoadClearMethod", Constants.clearMethodDelay);
StartCoroutine(SwitchTurnToNextPlayer(false, Constants.turnPlayerDelay));
}
/// <summary>
/// Method to display Card Swap on UI
/// </summary>
/// <param name="parent">Game Object of Current Player</param>
/// <param name="touchedCard">Touched card that need to be swapped</param>
/// <param name="animationHolder">Start point of Player Animation display</param>
/// <param name="isparentdistrubtioncompleted">True if Current Player all card distributed else False </param>
/// <returns></returns>
private IEnumerator SingleSwapAnimationFromDealtDeck(GameObject parent, Card touchedCard, GameObject animationHolder, bool isparentdistrubtioncompleted)
{
Debug.Log("Inside Single Swap Discarded Deck Animation Method");
foreach (Transform child in parent.transform)
{
CardUI cardUI = child.GetComponent<CardUI>();
if (cardUI.card == touchedCard)
{
var cover = Instantiate(cardUI, animationHolder.transform.position, Quaternion.identity, parent.transform);
cover.GetComponent<RectTransform>().sizeDelta = new Vector2(100, 100);
var tween = cover.transform.DOMove(GameView.Instance.discardedDeckObject.transform.position, 0.5f);
//https://stackoverflow.com/questions/32413067/unity2d-destroy-method-not-working-when-i-try-to-destroy-a-game-object
tween.OnComplete(() => Destroy(cover.gameObject));
yield return new WaitForSeconds(0.6f);
}
}
if (isparentdistrubtioncompleted)
{
var cardobject = GameView.Instance.dealtDeckObject.transform.GetChild(0);
var animationobject = Instantiate(cardobject, GameView.Instance.dealtDeckObject.transform.position, Quaternion.identity, GameView.Instance.dealtDeckObject.transform);
var tween1 = animationobject.transform.DOMove(animationHolder.transform.position, 0.5f);
tween1.OnComplete(() => Destroy(animationobject.gameObject));
yield return new WaitForSeconds(0.6f);
}
}
/// <summary>
/// Method to remove all the touched cards of Player
/// deck if they belong to removable rules,
/// and add them to discarded deck
/// and add top card of discarded deck to Player deck
/// </summary>
/// <param name="player"></param>
/// <param name="tempLongTouchedList"></param>
public void MultiCardDiscardedDeckSwap(Player player, Deck tempLongTouchedList)
{
if (HandCombination.IsStraight(player, tempLongTouchedList) == true || HandCombination.isThreeOfKind(player, tempLongTouchedList) == true)
{
tempLongTouchedList.SortByRankAsc();
Card temp = DiscardedDeck.Deal();
int i = 0;
int size = tempLongTouchedList.CardsCount();
while (i < size)
{
Card removeCard = player.RemoveCard(tempLongTouchedList.GetCardByIndex(i));
DiscardedDeck.Add(removeCard);
i++;
StartCoroutine(SingleSwapAnimationFromDiscardedDeck(PlayerUIMapping.Instance.cardholder[currentPlayerIndex], removeCard,
CardDistributionAnimation.instance.playersPosition[currentPlayerIndex], false));
}
StartCoroutine(SingleSwapAnimationFromDiscardedDeck(PlayerUIMapping.Instance.cardholder[currentPlayerIndex], null,
CardDistributionAnimation.instance.playersPosition[currentPlayerIndex], true));
player.AddToHand(temp);
tempLongTouchedList.Clear();
Invoke("LoadClearMethod", Constants.clearMethodDelay);
StartCoroutine(SwitchTurnToNextPlayer(false, Constants.turnPlayerDelay));
}
}
public IEnumerator SwitchTurnToNextPlayer(bool isShowDownCalled, float delayTime)
{
Debug.Log("Inside Switch Turn To Next Player Method");
yield return new WaitForSeconds(delayTime);
int size = players.Count;
if(isShowDownCalled)
{
for(int i=0;i<size;i++)
{
if(players[i].IsRoundWon())
{
currentPlayerIndex = (i + 1) % size;
}
}
}
else
{
currentPlayerIndex = (currentPlayerIndex + 1) % size;
}
players[currentPlayerIndex].NotifyPlayerForTurn();
Debug.Log("Switch Turn Method Executed Succussfully");
}
private void LoadClearMethod()
{
Debug.Log("Inside Load Clear Method");
StartCoroutine(IClearItems(PlayerUIMapping.Instance.cardholder[currentPlayerIndex]));
StartCoroutine(IClearItems(GameView.Instance.dealtDeckObject));
StartCoroutine(IClearItems(GameView.Instance.discardedDeckObject));
}
private IEnumerator IClearItems(GameObject content)
{
for (int i = 0; i < content.transform.childCount; i++)
{
Destroy(content.transform.GetChild(i).gameObject);
}
yield return new WaitUntil(() => content.transform.childCount == 0);
}
// Start is called before the first frame update
void Start()
{
if (!initialise)
InitialiseGame();
}
private void OnDisable()
{
Debug.Log("Inside Disable method");
StopAllCoroutines();
}
}
Log Screenshots
1st execution
2nd Execution
Comment
Your answer
Follow this Question
Related Questions
Why isn't my coroutine working when I call it from another script. 0 Answers
Stackoverflow by too many calculations? 2 Answers
Coroutines not running one after another 1 Answer
Need help using coroutines 1 Answer
c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers