- Home /
Need help writing script
I need some help with a fighting card game I'm developing. So far in the game, the player has access to a hand that holds 6 cards and a tabletop where they can present the card of their choice. Currently with the tabletop, you can add all of your cards into it, when I only want the player to be able to place one for every turn. I have an idea on how to write the script to prevent this from happening, but I'm a bit of a newbie to programming so I decided to reach out for help.
This is what I have so far:
void Update()
{
if (this.transform.childCount > 1)
{
//if the tabletop top has a card gameObject on it, then don't add another one
}
}
I tried to keep this question short to fix my mistake in asking for last time, so I'm sorry if I went too vague. Let me know if there is any information you need and I'll be happy to oblige.
Answer by Aviryx · Sep 10, 2020 at 11:12 PM
That would technically run once per frame which is inefficient. Have you considered trying some other kind of implementation? I don't exactly know how you are doing all of this but something like this might work.
public class TableTop : MonoBehaviour
{
// maybe you want to know whos turn it is.
public int activePlayer;
// the maximum amount of cards that can be played this turn
public int maximumPlayableCards;
// keep track of how many cards the active player has placed on the table
public int activePlayerCardsPlacedOnTable;
private void PlaceCardOnTable()
{
if (activePlayerCardsPlacedOnTable < maximumPlayableCards)
{
// let them place the card and then increment activePlayerCardsPlacedOnTable by 1
EndActiveCardPlacementPhase();
}
}
private void EndActiveCardPlacementPhase()
{
// do something when the player has placed their card
activePlayer = 2; // now it's player 2's turn to place a card?
maximumPlayableCards = 1; // they can place 1 card
activePlayerCardsPlacedOnTable = 0; // they have not placed any cards yet
}
}
Thank you for replying so quickly. I put the code inside the TableTop script, but it seems to not be doing anything. I copied the values from your script into the script component in the inspector since it was zeros across the board, but I was still able to place multiple cards in the tabletop gameObject.
If it would help, I'll paste some of the scripts, how it functions and what gameObjects it's attached to below.
For moving the cards, I have Draggable that deals with picking up cards from the player hand and/or tabletop, and is attached to my card prefabs:
public class Draggable : $$anonymous$$onoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Transform parent = null;
public Transform parentToReturnTo = null;
public void OnBeginDrag(PointerEventData eventData)
{
parentToReturnTo = this.transform.parent;
this.transform.SetParent(this.transform.parent.parent);
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
this.transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
this.transform.SetParent(parentToReturnTo);
GetComponent<CanvasGroup>().blocksRaycasts = true;
}
public void TimerOver()
{
}
}
For placing cards, I have DropZone that's attached to the player's hand and the tabletop, and it makes the card a child to whichever object it's on top of:
public class DropZone : $$anonymous$$onoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
public void OnPointerEnter(PointerEventData eventData)
{
}
public void OnPointerExit(PointerEventData eventData)
{
}
public void OnDrop( PointerEventData eventData)
{
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if(d != null)
{
d.parentToReturnTo = this.transform;
}
}
}
If this isn't enough information, I can post a screenshot of my game scene and hierarchy.
Thank you again for the help.
Edit: I just realized, it'd probably be best if I said I put a Horizontal Layout Group component on the tabletop, to make sure the card snapped to the center.
Your answer
Follow this Question
Related Questions
Slowly increase joint limit over time 1 Answer
Move from A to B after recieving touch input 1 Answer
UnityEngine.UI.dll 0 Answers
Why does my player not move? 1 Answer
How to double spirte/gameobject/prefab and control the result on those items? 0 Answers