ArguentOutOfRangeException: Argument is out of range. Parameter name:index
Hi, up until recently I had some code that was working fairly well. I'm not sure what I did, but now it's throwing an ArgumentOutOfRangeException and I have no idea what is causing it. Can anyone help me?
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
public class SearchMap : MonoBehaviour {
public float tagPercent = 80;
private float inputRatio;
private int q;
private int b;
private locationDatabase database;
public List<Location> seenLocations = new List<Location>();
public List<Location> newLocations = new List<Location> ();
private List<Location> savedLocations = new List<Location> ();
public List<string> tagList = new List<string> ();
public string[] tagArray;
public GameObject locListItem;
public GameObject viewContent;
public GameObject locMenu;
public GameObject spawnItem;
public GameObject curPip;
public Transform pipHolder;
public InputField iField;
public string myName;
public string tinyTag;
public string[] tags;
openLocation oLocation;
public Sprite bluePip;
public Sprite redPip;
private bool tagLength;
public void bounceName()
{
foreach(Transform child in pipHolder)
{
child.gameObject.SetActive(false);
}
myName = iField.text.ToLower ();
foreach (Transform child in viewContent.transform)
{
Destroy (child.gameObject);
}
foreach (Location loc in database.locations)
{
tagLength = false;
tagArray = database.locations[b].locTags.Split(',');
q = 0;
foreach(string tag in tagArray)
{
if(tagArray[q].Contains(myName) && (myName.Length * 1f)/(tagArray[q].Length * 1f) >= tagPercent/100)
{
Debug.Log(tagArray[q]);
tagLength = true;
break;
}
q++;
}
Debug.Log("before suspect");
Debug.Log(database.locations[b]);
if ((!seenLocations.Contains (database.locations [b])) && savedLocations.Contains (database.locations [b]) && tagLength)
{
seenLocations.Add (database.locations [b]);
Debug.Log("Added: " + seenLocations[b]);
b++;
}
else if ((!seenLocations.Contains (database.locations [b])) && !savedLocations.Contains (database.locations [b]) && tagLength)
{
newLocations.Add (database.locations [b]);
Debug.Log("Added: " + newLocations[b]);
b++;
}
else
{
b++;
Debug.Log("Went to C Round");
}
Debug.Log("Stuck at end of part I");
}
Debug.Log ("Made it end of part I");
b = 0;
foreach (Location loc in newLocations) {
Debug.Log("In part 2");
spawnItem = Instantiate (locListItem) as GameObject;
spawnItem.transform.SetParent (viewContent.transform, false);
spawnItem.transform.localPosition = new Vector3 (100, -39 - (70 * b), 0);
oLocation = spawnItem.GetComponent<openLocation> ();
oLocation.locName = newLocations [b].locName;
oLocation.locAddress = newLocations [b].locAddress;
oLocation.locDescription = newLocations [b].locInfo;
oLocation.locScene = newLocations[b].locScene;
oLocation.pipNumber = (b + 1).ToString();
oLocation.newPip = true;
oLocation.locMenu = locMenu;
curPip = pipHolder.FindChild(newLocations[b].locName).gameObject;
curPip.gameObject.transform.GetComponent<Image> ().sprite = redPip;
curPip.transform.GetChild(0).GetComponent<Text>().text = (b + 1).ToString();
curPip.transform.GetComponent<Pip>().listButton = spawnItem;
curPip.SetActive(true);
b++;
}
b = 0;
foreach (Location loc in seenLocations) {
spawnItem = Instantiate (locListItem) as GameObject;
spawnItem.transform.SetParent (viewContent.transform, false);
spawnItem.transform.localPosition = new Vector3 (100, -39 - (70 * (b + newLocations.Count)), 0);
oLocation = spawnItem.GetComponent<openLocation> ();
oLocation.locName = seenLocations [b].locName;
oLocation.locAddress = seenLocations [b].locAddress;
oLocation.locDescription = seenLocations [b].locInfo;
oLocation.locScene = seenLocations[b].locScene;
oLocation.pipNumber = (b + 1).ToString();
oLocation.newPip = false;
oLocation.locMenu = locMenu;
curPip = pipHolder.FindChild(seenLocations[b].locName).gameObject;
curPip.gameObject.transform.GetComponent<Image> ().sprite = bluePip;
curPip.transform.GetChild(0).GetComponent<Text>().text = (b + 1).ToString();
curPip.transform.GetComponent<Pip>().listButton = spawnItem;
curPip.SetActive(true);
b++;
}
b = 0;
foreach (Location loc in seenLocations) {
savedLocations.Add (seenLocations [b]);
b++;
}
seenLocations.Clear();
b = 0;
foreach (Location loc in newLocations) {
savedLocations.Add (newLocations [b]);
b++;
}
newLocations.Clear();
}
//Debug.Log (myName);
void Start()
{
database = GetComponent<locationDatabase> ();
}
}
The locations themselves just hold a bunch of strings, the last of which is a list of tags the player can search for, separated by commas. Here's the full error:
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Location].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) SearchMap.bounceName () (at Assets/Scripts/testing/SearchMap.cs:75) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:149) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:626) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:766) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:54) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Answer by jgodfrey · Mar 11, 2016 at 05:02 PM
That's a lot of code and not much detail on the problem. You need to post the full error message so we have some idea where the error is in the posted code...
Your answer
Follow this Question
Related Questions
Unknown Argument Out of Range Index Error On Card Game 1 Answer
Why is argument out of range? 1 Answer
How to compress iterative code, perhaps using a list and/or loops? 1 Answer
Get all Children of an Object with a certain component 0 Answers
SOLVED : How to scan and delete elements from a name/score array on a dreamlo database 2 Answers