0
\$\begingroup\$

I hve that script:

public void ActivateCard(Card card)
        {
            Debug.Log("Activating card: " + card.CardsData);
            if (card.CardsData.creatureName != string.Empty)
                Summon(card);
            else
                CastSpell(card);

            if (_hand.Cards.Contains(card))
            {
                _hand.Cards.Remove(card);
                Debug.Log("Card " + card.CardsData.cardName + " deleted");
                Debug.Log("now we have(cards): " + _hand.Cards.Count);
            }
                

            _hand.TakingCards(0);
        }

And I expect that card will be removed from the list. But it allways remove only FIRST element in there. Here's what I have(card can't be the same in one list): enter image description here

ADDICTION:

It's not a full code, but it's all we need I think. Here's how this method calls: -I-

public class InputClass
{
  void Update
  {
    if (targetSet)
     {
         if (Input.touches[0].phase == TouchPhase.Ended)
                    Cancel(true);
     }
  }

  void Cancel(bool isDone){ _actCard.Cancel(isDone); }
}

-II-

class ActCard
{
  public void Cancel(bool isDone)
  {
    if (isDone)
     {
        _activator.ActivateCard(_card);
     }
  }
}

-III-

class CardActivator
{
  public void ActivateCard(Card card)
        {
            Debug.Log("Activating card: " + card.CardsData);
            if (card.CardsData.creatureName != string.Empty)
                Summon(card);
            else
                CastSpell(card);

            if (_hand.Cards.Contains(card))
            {
                _hand.Cards.Remove(card);
                Debug.Log("Card " + card.CardsData.cardName + " deleted");
                Debug.Log("now we have(cards): " + _hand.Cards.Count);
            }
        }
}

-IV-

public class Hand
{
  List<Card> _hand = new List<Card>();
  
  public List<Card> Cards
        {
            get { return _hand; }
            set { _hand = value; }
        }
}
```
\$\endgroup\$
9
  • \$\begingroup\$ Did you implement IEquatable or override Equals() and GetHashCode() in your Card class? \$\endgroup\$
    – Zibelas
    Commented Dec 23, 2020 at 10:11
  • \$\begingroup\$ @Zibelas no... What exactly should I do? \$\endgroup\$
    – Dead Lord
    Commented Dec 23, 2020 at 10:14
  • \$\begingroup\$ Basically contains does not know how to identify the correct card in your list since you didn't implement those. You can take a look at this stackoverflow.com/questions/2629124/… Your equals should return something that is unique for all your cards, like an id, name or a combination. \$\endgroup\$
    – Zibelas
    Commented Dec 23, 2020 at 10:15
  • \$\begingroup\$ @Zibelas oh! I see! Even if I have card.CardsData.cardsName it doesn't counts, right? \$\endgroup\$
    – Dead Lord
    Commented Dec 23, 2020 at 10:20
  • \$\begingroup\$ You can use that for your id, but the List contains function does not know you want this or that it can use this as identifier. It should be as simple as override the Equals in your Card class and just return the cardsName. Another thing, if you have twice the same card, player uses the second, would it matter if the first gets remove? \$\endgroup\$
    – Zibelas
    Commented Dec 23, 2020 at 10:22

1 Answer 1

1
\$\begingroup\$

Basically contains does not know how to identify the correct card in your list since you didn't implement those. You can take a look at this stackoverflow.com/questions/2629124/… Your equals should return something that is unique for all your cards, like an id, name or a combination.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .