-3
//Script 1 
  if (other.gameObject.tag == "Hat")
        {                    
            sendPickValues.Invoke(0, 1);
        }
if (other.gameObject.tag == "Boot")
            {                    
                sendPickValues.Invoke(4, 1);
            }

//Script 2
 private void OnEnable()
    {
                WearableCollider.sendPickValues += GetPickValues;
    }

    List<int> getVal = new List<int>();
    public void GetPickValues(int selection, int toadd)
        {        
            getVal.Insert(selection,toadd);       
        }

I think the first value is entered after calling GetPickValues second time getting error "ArgumentOutOfRangeException: Insertion index was out of range. Must be non-negative and less than or equal to size. Parameter name: index"

Aim is to store by taking specific index and storing values inside it.I may take getVal[0] and save -1 and next I need to take getVal[5]=1. Insert function is used to insert at a specific position right?

zyonneo
  • 1,319
  • 5
  • 25
  • 63
  • how do you invoke `GetPickValues`? maybe you pass to it index that doesn't exist – Omri Attiya Aug 28 '20 at 09:10
  • The indexes must be an increment of 0, you can't insert 0 followed by 2. Make sure you start at 0 and increment by one with each insert. – Adrian Aug 28 '20 at 09:11
  • I want to store 1 and -1 depending on the objects a user chooses.if he chooses first object it should save in 0th index.Similarily user can pick 5th object it should be saved in the 4th index. – zyonneo Aug 28 '20 at 09:33
  • @OmriAttiya tried with arraylist by specifying size....still same issue.Looks like need to use dictionary – zyonneo Aug 28 '20 at 11:57
  • @zyonneo please actually show the code (how you invoke the function) that throw the error – Omri Attiya Aug 28 '20 at 12:00
  • @OmriAttiya Updated Code... – zyonneo Aug 28 '20 at 12:40

1 Answers1

1

When you first run your code, you initialize a new (empty) List :

List<int> getVal = new List<int>();

Since the list have 0 items in it, you cant insert a value at index that does not exists. If you want to simple add items to the list use Add() instead of Insert(), without an index.

getVal.Add(toadd);  

If you don't want it to be added like this and you want to put something at a specific index, I suggest you to use a Dictionary

Omri Attiya
  • 3,917
  • 3
  • 19
  • 35