Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 matthew_gigante · Oct 20, 2017 at 06:04 PM · c#liststringargumentoutofrangeexceptionglobal variable

Trouble with removing items from a list by string

I have a sellScript attached to a few different buttons, when pressed, each button will search for the first list item with a certain string name, then remove it from the list, or do nothing if the list has no such string. My issue is that when I'm selling multiple items, the last item sold does not count towards coin count, does not come up in Debug.Log (itemsFound [inventoryNumber]); but if for example there are two books in inventory, one can be sold and 2 gold is added, and for the next one there is no coin gain and ArgumentOutOfRangeException: Argument is out of range is displayed.

My code is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class sellScript : MonoBehaviour {
     public List<string> itemsFound;
     public int coins;
     public int inventoryNumber = 0;
     //all items possible in the game can be used here, this will act as a button attached to different scripts
     public string functionToCall;
     public int inventoryCapacity;
     public string stringToFind;
     public AudioSource sellSound;
 
 
     void Start () {
         itemsFound = globalControl.Instance.itemsFound;
         coins = globalControl.Instance.coins;
     }
 
     void OnMouseDown(){
         inventoryNumber = 0;
         Invoke (functionToCall, 0f);
     }
 
     // A book.
     void book(){    
         inventoryCapacity = globalControl.Instance.inventoryCapacity;
         itemsFound = globalControl.Instance.itemsFound;
         coins = globalControl.Instance.coins;
         if(inventoryNumber < itemsFound.Count && inventoryNumber < 20){
 
             if(itemsFound[inventoryNumber].Contains(stringToFind)){
                 itemsFound.Remove(itemsFound[inventoryNumber]);
                 Debug.Log (itemsFound [inventoryNumber]);
                 sellSound.Play ();
                 coins = coins + 2;
                 if (inventoryNumber != 0) {
                     inventoryCapacity--;
                 }
                 globalControl.Instance.inventoryCapacity = inventoryCapacity;
                 globalControl.Instance.itemsFound = itemsFound;
                 globalControl.Instance.coins = coins;
             }
             if (itemsFound[inventoryNumber].Contains (stringToFind) == false) {
                 if (inventoryNumber < 20) {
                     inventoryNumber++;
                     book();
                 }
                 if (inventoryNumber > 20) {
                     Debug.Log ("end of function");
                 }
             }
         }
     }
 
     // A silver watch.
     void silverWatch(){    
         inventoryCapacity = globalControl.Instance.inventoryCapacity;
         itemsFound = globalControl.Instance.itemsFound;
         coins = globalControl.Instance.coins;
         if(inventoryNumber < itemsFound.Count && inventoryNumber < 20){
 
             if(itemsFound[inventoryNumber].Contains(stringToFind)){
                 itemsFound.Remove(itemsFound[inventoryNumber]);
                 Debug.Log (itemsFound [inventoryNumber]);
                 sellSound.Play ();
                 coins = coins + 5;
                 if (inventoryNumber != 0) {
                     inventoryCapacity--;
                 }
                 globalControl.Instance.inventoryCapacity = inventoryCapacity;
                 globalControl.Instance.itemsFound = itemsFound;
                 globalControl.Instance.coins = coins;
             }
             if (itemsFound[inventoryNumber].Contains (stringToFind) == false) {
                 if (inventoryNumber < 20) {
                     inventoryNumber++;
                     silverWatch();
                 }
                 if (inventoryNumber > 20) {
                     Debug.Log ("end of function");
                 }
             }
         }
     }
 
     // Cloth strip.
     void clothStrip(){    
         inventoryCapacity = globalControl.Instance.inventoryCapacity;
         itemsFound = globalControl.Instance.itemsFound;
         coins = globalControl.Instance.coins;
         if(inventoryNumber < itemsFound.Count && inventoryNumber < 20){
 
             if(itemsFound[inventoryNumber].Contains(stringToFind)){
                 itemsFound.Remove(itemsFound[inventoryNumber]);
                 Debug.Log (itemsFound [inventoryNumber]);
                 sellSound.Play ();
                 coins++;
                 if (inventoryNumber != 0) {
                     inventoryCapacity--;
                 }
                 globalControl.Instance.inventoryCapacity = inventoryCapacity;
                 globalControl.Instance.itemsFound = itemsFound;
                 globalControl.Instance.coins = coins;
             }
             if (itemsFound[inventoryNumber].Contains (stringToFind) == false) {
                 if (inventoryNumber < 20) {
                     inventoryNumber++;
                     clothStrip();
                 }
                 if (inventoryNumber > 20) {
                     Debug.Log ("end of function");
                 }
             }
         }
     }
 
     // Shard of glass.
     void shardOfGlass(){    
         inventoryCapacity = globalControl.Instance.inventoryCapacity;
         itemsFound = globalControl.Instance.itemsFound;
         coins = globalControl.Instance.coins;
         if(inventoryNumber < itemsFound.Count && inventoryNumber < 20){
 
             if(itemsFound[inventoryNumber].Contains(stringToFind)){
                 itemsFound.Remove(itemsFound[inventoryNumber]);
                 Debug.Log (itemsFound [inventoryNumber]);
                 sellSound.Play ();
                 coins++;
                 if (inventoryNumber != 0) {
                     inventoryCapacity--;
                 }
                 globalControl.Instance.inventoryCapacity = inventoryCapacity;
                 globalControl.Instance.itemsFound = itemsFound;
                 globalControl.Instance.coins = coins;
         }
             if (itemsFound[inventoryNumber].Contains (stringToFind) == false) {
                 if (inventoryNumber < 20) {
                     inventoryNumber++;
                     shardOfGlass ();
                 }
                 if (inventoryNumber > 20) {
                     Debug.Log ("end of function");
                 }
             }
     }
 }
 }
 
Comment
Add comment · Show 8
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 matthew_gigante · Oct 20, 2017 at 06:10 PM 0
Share

EDIT: something that may help is that the inventory, or itemsFound, is supposed to contain a max of 20 items.

avatar image Stevens-R-Miller · Oct 20, 2017 at 06:36 PM 0
Share

Quick try from me: At Line 45, you are accessing itemsFound by using inventoryNumber as an index, which is at least 0. But are you sure at that point that itemsFound is not empty? If it is of length zero, indexing at position zero (which will only work if itemsFound is of length one or more) would cause the exception you are seeing.

avatar image matthew_gigante Stevens-R-Miller · Oct 20, 2017 at 09:21 PM 0
Share

itemsFound contains 1 item (element 0) I noticed that I also get a ArgumentOutOfRangeException even if i manually put in one or more list items at the bottom of the list, it only calls this exception when the last items in my script are sold "removed from list"

avatar image TreyH · Oct 20, 2017 at 06:46 PM 0
Share

not to give unsolicited advice, but is there a reason you are making a function for each specific item?

avatar image matthew_gigante TreyH · Oct 20, 2017 at 09:21 PM 0
Share

this script is attached to buttons, I'm challenging myself to make my code as reusable as possible, so it can be attached to many buttons and still do different things.

avatar image MaxGuernseyIII · Oct 20, 2017 at 06:48 PM 0
Share

This is not a question. This is a problem. The point of the Q&A part of the community is to make it so that recurring questions have their answers captured. If you just want help working through a problem, it seems like the forum is the place to get it.

It also makes it easier for us to help, because we can have a nice, linear conversation with you.

avatar image matthew_gigante MaxGuernseyIII · Oct 20, 2017 at 09:22 PM 0
Share

I suppose its more of a question on why I am getting an exception, when I have a list that seems to be in order. Im only asking about a small portion of my code, and it was too unique to be considered a recurring question.

avatar image MaxGuernseyIII matthew_gigante · Oct 20, 2017 at 09:42 PM 0
Share

You're going to get better engagement on the forums than on this medium. Look at how fractured these conversations are. They split off in all directions. Up and down-votes mess with the order. If you want help through a problem with your code, I've generally observed that the community is happy to answer it on the forums. On this medium, you may get takers but it's going to be harder for us to help and harder for you to rationalize what we tell you.

0 Replies

· Add your reply
  • Sort: 

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

408 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot implicitly convert type string to list - Why is this? 1 Answer

cannot make a list with the word hello in it 1 Answer

A node in a childnode? 1 Answer

Multiple Cars not working 1 Answer

How can I get just one componente, or except one, from a list? (string.Join) 0 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