I’m currently running into an issue with a script that keeps track of characters nearby my player character by using a Collider and a List, but when I’m trying to get character data from the list, it gives me an OutOfRange error. In the editor, the list is populating, but when trying to access through the script, it just gives me the error. I’ve tried setting the List to static and checked the editor for other instances of the object, but I cannot find any additional versions of this script.
//Sending selection
public GameObject makeSelection()
{
//Cleaning up
Destroy(selector);
//If making a selection, menu style
if (ActivelySelecting)
{
ActivelySelecting = false;
return CharacterSelections[Index];
}
//If moving around and need to select a character
Debug.Log("Passive List: " + PassiveSelections.Count);
if (PassivelySelecting)
{
PassivelySelecting = false;
return PassiveSelections[Index]; //Index should be 0 [Error Here]
}
Debug.Log("Error in making a selection.");
return null;
}
//When Ally/Enemy enters activation area
private void OnTriggerEnter2D(Collider2D collision)
{
//if Avatar needs to be close to a character to select
if (PassivelySelecting)
{
//Debug.Log("GameObject has Entered");
//Determining which types of character should be added
switch (CharactersToLookFor)
{
case "Ally":
if (collision.CompareTag("Ally"))
{
PassiveSelections.Add(collision.gameObject);
//PassiveSelections.TrimExcess();
if (PassiveSelections.Count == 1)
selector = Instantiate(SelectorIcon, PassiveSelections[0].transform.position, Quaternion.identity);
Debug.Log("Ally has entered radius.");
}
break;
case "Enemy":
if (collision.CompareTag("Enemy"))
{
PassiveSelections.Add(collision.gameObject);
//PassiveSelections.TrimExcess();
if (PassiveSelections.Count == 1)
selector = Instantiate(SelectorIcon, PassiveSelections[0].transform.position, Quaternion.identity);
Debug.Log("Enemy has entered radius.");
}
break;
case "Any":
if (collision.CompareTag("Ally") || collision.CompareTag("Enemy"))
{
PassiveSelections.Add(collision.gameObject);
//PassiveSelections.TrimExcess();
if (PassiveSelections.Count == 1)
selector = Instantiate(SelectorIcon, PassiveSelections[0].transform.position, Quaternion.identity);
Debug.Log("Character has entered radius.");
}
break;
default:
Debug.Log("Error: Incorrect Selection Type String was Passed.");
break;
}
}
}
//When Ally/Enemy exits activation area
private void OnTriggerExit2D(Collider2D collision)
{
if (PassivelySelecting)
{
//Removing character from list
PassiveSelections.Remove(collision.gameObject);
PassiveSelections.TrimExcess();
//checking if another character should be selected
if (PassiveSelections.Count > 0)
selector.transform.position = PassiveSelections[0].transform.position;
else
Destroy(selector);
Debug.Log(collision.tag + " has exited radius.");
}
}