# list argument out of range

I am working on a card game and have encountered a problem with a lists and argument out of range exception, parameter Index.
This drives me insane and I am out of any ideas how to solve this…
I will be extremely grateful…
The most confusing thing is that my code works 8 out of 10 times …
Same code will work for a while then it will for no apparent reason crash and cause argument out of range exception error…

I have a deck of cards which is a list
Also player hands are a list…

So when AI player wants to check if he has an ACE in his hand ,
He checks his first and highest card in each suite… ( this works because cards are sorted )
So if player has an ace it will be the very first card in a suite…
Gets the value for that card and if the value matches the ACE value which is 15
We make int Aces plus 1….
So code goes like this

``````    if (DiamondCardP3 > 0) // if player 3 has any Diamond Cards
{
int possibleDiamondAceCard = 0;
// we know first card will have index of zero in the list, and if there is an Ace it will be first card
int diamondAce = p3.cards[possibleDiamondAceCard];

// here we assign a value of the first card in players3 , cards list to DiamondAce int

// Table is also a list , so we play that card from players hand and place it in table list…
``````

The rest of code is not important …. Because crash happenes normally when I assign the value to int diamondAce…
Is this not a way to get a value of an element from the list ? what am I doing wrong?
Most annoying thing is this actually works most of the time … I don’t get this error offten

thank you Bonfire Boy so much for this suggestion, this has finally added some light to whats going on …
even though it does not fix the problem

I have used + “Card count is” +p3.cards.Count as you suggested
and for the first 5 rounds , everything was running ok and card count was alway 13 as it should be…
this is a bidding faze when players have all 13 cards …

but then in the very last round game chrashed and my card count was down to 8 … even though if i check inspector all 13 cards are showing there on player 3 script … makes no sense …
it seems that gamemanager is only aware of 8 cards the rest 5 are missing,
and why would gamemanger only see 8 cards ? why not all or none ?
how does that happen?
I am very confused …

This error is on a main script which is athe gamemanager script , card list is on the player object p3…
is it passible that during game , gamemanager loses access to player 3 script and his card list?
if so how can i prevent this?

also game does not chrash every time … sometimes it all works to the end …

if anyone has an idea please let me know