Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Grinch91 · Mar 18, 2014 at 03:54 PM · argumentoutofrangeexception

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CodeElemental · Mar 18, 2014 at 03:58 PM 1
Share

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.

avatar image Grinch91 · Mar 18, 2014 at 04:02 PM 0
Share

Yea I found my issue. I had a deck.clear() I forgot to comment out/delete that was causing things to not work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges