I’m new in Unity and i started making my first game which is a turn based card game.
I’m writing The GameManager Script now that will run the Player1Turn and Player2Turn functions that will give control to the players once the drop panel area gets the dropped card as a child, i made a flag boolean variable and an int Whos_Turn (1 or 2) variable for that
And here is the management code ( Sorry it’s a bit long ):
private void EnablePlayer1Control() // Giving Control To player1
{
if (Whos_Turn == 1)
{
foreach (GameObject card in P1Hand)
{
card.GetComponent<Card>().Draggable = true;
}
}
}
private void EnablePlayer2Control() // Giving Control To Player2
{
if (Whos_Turn == 2)
{
foreach (GameObject card in P2Hand)
{
card.GetComponent<Card>().Draggable = true;
}
}
}
private void DisablePlayer1Control()
{
if (Whos_Turn == 2)
{
foreach (GameObject card in P1Hand)
{
card.GetComponent<Card>().Draggable = false;
}
}
}
private void DisablePlayer2Control()
{
if (Whos_Turn == 1)
{
foreach (GameObject card in P2Hand)
{
card.GetComponent<Card>().Draggable = false;
}
}
}
private void SetWhosTurn()
{
if (Flag2)
{
Whos_Turn = 1;
}
if (Flag1)
{
Whos_Turn = 2;
}
}
private void Player1Turn()
{
Flag2 = false;
EnablePlayer1Control();
DisablePlayer2Control();
Debug.Log("Player1Turn");
if (Flag1)
{
Debug.Log("Player1 Played A Card and Ended his turn !");
return;
}
}
private void Player2Turn()
{
Flag1 = false;
EnablePlayer2Control();
DisablePlayer1Control();
Debug.Log("Player2Turn");
if (Flag2)
{
Debug.Log("Player2 Played A Card and Ended his turn !");
return;
}
}
private IEnumerator PlayRound()
{
Player1Turn();
if (Whos_Turn == 2)
{
yield return 1;
Player2Turn();
}
yield return null;
private IEnumerator GameLoop()
{
yield return PlayRound();
}
void Start()
{
StartCoroutine((GameLoop()));
}
void Update()
{
SetWhosTurn();
if (P1DropArea.transform.childCount == Default_Num + 1) //if the drop area panel gets a card
{
Flag1 = true;
}
if (P2DropArea.transform.childCount == Default_Num + 1) //if the drop area panel gets a card
{
Flag2 = true;
PlayedCard = P2DropArea.transform.GetChild(Default_Num).gameObject;
}
When i play the scene, all public variables ( Whos_Turn, the flags ) are changed the way i programmed them on the script, but the problem is, the function Player1Turn() does not respond to the condition if (Flag1 //which means if the player dropped the card) and the message i set up in the Debug.Log ““Player1 Played A Card and Ended his turn !”” ( line 69) does not appear, which means the function is still running and so the player 2 can’t play its turn.
Just reading on my mobile, but isnt your update checking if player 1 is dropping a card and if he can drop a card in the same way? So player1 plays, sets flag1 to true and then player2turn sets it back to false?
Let me know if that unity package is insightful at all.
I tried to take what I could gather from your post, and put a very simple sample together.
There are 2 players. First it’s player one’s turn, and they can only drop into their own area. Player 2’s cards cannot be moved until player 1 has finished.
It’s very basic… for instance, if either player changes their mind and wants to play another card, they can’t really put their original choice (for dragging) back as a child of their pile.
Still, hopefully something there is helpful. It should be pretty easy to read, I think/hope.
They can also drag from the dropped area. lol but again, that could be fixed.
The Update checks if the Player1_DropPanel (P1DropArea) gets a new card, which means player1 dropped a card, that’s what’s the flag for. That way i can end the playerturn in the Player1Turn(), then when the Player2 turn is activated, the flag of The Player1_DropPanel is set back to false.
The variables work fine, i made them public and i watched them on the inspector as i drop the first for the player1turn but the turn does not end and the player2 can’t play or drag his cards.
Actually line 117 is executed, the Whos_Turn variable changes from 1 to 2 once the first card is dropped by Player1. The problem is with the line 61 i think, since the Player1Turn() doesn’t end at all. The line 67 is the one that doesn’t get executed.
for the childcount, i checked it and it’s working, after the card is dropped, it increases by 1. Very weird
Actually line 117 is executed, the Whos_Turn variable changes from 1 to 2 once the first card is dropped by Player1. The problem is with the line 61 i think, since the Player1Turn() doesn’t end at all. The line 67 is the one that doesn’t get executed.
for the childcount, i checked it and it’s working, after the card is dropped, it increases by 1. Very weird
The problem is, your script jumps forth and back, which does not make a clear structure of what is when going on. I bet somethingnis resetting your value flag before it gets realized by other code. Did you try to delete update and use a coroutine with a while loop instead? While childcount is lower , wait for player, when is equal return and end coroutine, start the new one for player 2 and so on.