- Home /
Argument out of Range
I am having a little trouble fixing this small problem in my code. Firstly the game is a card game, and my trouble occurs when I go to deal a card from the deck. I have checked if the deck actually contains information, which is it does. So on first start the game.cs initialize the different decks and adds the cards into each. Again I have checked if the information is being passed which it is.
Once the start function has finished the game state is changed to initial which then runs a function for setting up the player's and enemy's hands.
Game.cs
void InitialSetUp()
{
for(int i = 0; i<5;i++)
{
AddToEnemyHand();
AddToPlayerHand();
}
FlipCoin();
m_state = GameState.Begin;
}
void AddToPlayerHand()
{
List<System.Object> c1 = new List<System.Object>();
playerDeck.Deal (c1);
This seems to be working fine up until it gets to the last part I have included in the code. Once it gets to playerDeck.Deal(c1) and calls the deal function I get the ArgumentOutOfRange error.
public System.Object Deal(List<System.Object> returnCard)
{
returnCard.Add(deck[0]);
deck.RemoveAt(0);
return returnCard;
}
A deck is a list of cards, and the reason it is currently System.Object is due to the fact that different types of cards have different classes, i.e. solider class, active class, magic class, etc. I plan on changing this. Anyway I haven't been able to figure out why I am getting this error.
Some help or a fresh set of eyes would be extremely appreciated.
It is most probably due to that the "deck" variable does not have elements in it, so removeAt(0) fails with ArgumentOutOfRange exception.
Re-check the variable state in a debug session to pin-point the exact problem in your specific workflow.
Yea I found my issue. I had a deck.clear() I forgot to comment out/delete that was causing things to not work.
Answer by wibble82 · Mar 18, 2014 at 03:59 PM
An argument out of range exception generally means you're trying to access an index in an array or list that does not exist. For example, you could be trying to access (or remove) the 10th element in a list, when it's only 5 long. This would cause an argument out of range exception.
I suspect from your code that for some reason your 'deck' list is empty. You could verify this by adding 'Debug.Log(deck.Count)' just before you access 'deck' in the first line of 'Deal'. This should print out the length of the deck list, which I suspect will be 0 :)
Your answer
Follow this Question
Related Questions
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index 0 Answers
Trouble with removing items from a list by string 0 Answers
JS Argument out of range 1 Answer
I have an ArgumentOutOfRangeException that won't tell me where it is happening in the code. 0 Answers
Scrolling inventory script giving argument out of range. 2 Answers