Reputation: 295
Hi I I'm trying to create a card game and I have a class called hand, this hand has a public List<GameObject> cards;
and a methods:
public void AddCard(GameObject card)
{
cards.Add(card);
GameObject cardObject = Instantiate(card, startPosition, actualCardRotation);
}
public GameObject RemoveCard(GameObject card)
{
cards.Remove(card);
return card;
}
then I have a cardGameController attributes public Hand playerHand, computerHand;
that has a function to select a card for play and call RemoveCard function but its not working here is the code:
public void SelectCardToPlay()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.collider != null && hit.collider.CompareTag("Card"))
{
Console.text = hit.collider.gameObject.GetComponent<Card>().description;
if (Input.GetMouseButtonDown(0))
{
playedPlayerCard = playerHand.RemoveCard(hit.collider.gameObject);
playerSelectedCardBoard.AddCard(playedPlayerCard, 3);
playedComputerCard = computerHand.PickRandomCard();
computerSelectedCardBoard.AddCard(playedComputerCard, 3);
gameState = 5;
}
}
}
Seems like the object i get by hit.collider isn't the same as the hand list or something like that, any hints?
Upvotes: 1
Views: 40
Reputation: 5108
In your AddCard you're adding the input card, which I assume is a prefab - since you use it to spawn a new card. I then assume it's this spawned card that you want to remove from the list, since that's probably the object you're hitting?
Simply change your AddCard to:
public void AddCard(GameObject card)
{
GameObject cardObject = Instantiate(card, startPosition, actualCardRotation);
cards.Add(cardObject);
}
And see if that works.
If that wasn't the issue, make sure you debug exactly what object it is you're getting att all points. Add some Debug.Log(gameObject.name);
here and there to see.
Upvotes: 1