Card game : GameManaging Script, Making the cards in each player hand draggable

Hello everyone.

I’m new in Unity and i want some help in writing the script for my first game.

I wrote a script with a public static boolean variable Draggable and i added it to the card prefab as a component.

Now i’m writing the GameManager Script.
Here is the Hierarchy for the project :

3553489--285913--Sans titre.png

Now i’m writing the GameManager script. Since it’s a turn based card game i want Only the cards in the player1hand to be draggable during his turn. So i thought i have to acces the Player1_Hand GameObject children ( which are the cards ) and make them iterable ( array or list ), and depending on which player turn it is i will set the Draggable boolean for the cards to True or false.

So basically i need to know how to :
- Make an iterable variable ( List, Array ) Called PlayerXHand that contains the cards for PlayerX.
- For each card, Change the public static Draggable boolean which is declared on another script component “Card” attached to the Card Prefab.

Since i’m a beginner in game developping and c#, this is a bit difficult so i’m looking forward to your help.
Thank you

If you’re unsure how to use Lists / arrays, you should look them up. They are part of C#. Try googling “C# list” or “C# array”. This is Unity’s help for lists and dictionaries: Lists and Dictionaries - Unity Learn

I would say have a (non-static) bool for draggable that you can change on the script that’s on each card game object. A game manager could store 2 lists, one for each player. When the turns switch, turn off the current list and enable the new one.
Another option would be to store the current player’s turn, maybe as an integer, and the script could have a variable which keeps track of which player it belongs to. Then, when it’s trying to be dragged, you would compare the current player’s turn with the one on the script, allowing dragging only if they match.

I hope that helps for some ideas.

1 Like