- Home /
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");
}
}
}
}
}
EDIT: something that may help is that the inventory, or itemsFound, is supposed to contain a max of 20 items.
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.
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"
not to give unsolicited advice, but is there a reason you are making a function for each specific item?
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.
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.
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.
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.