Trying to fix an ArgumentOutOfRangeException

Howdy, I was (and still am) attempting to fix an ArgumentOutOfRangeException issue in my TopDownController script. My question would be where I went wrong, and how to patch it up.

It’s on line 56 apparently.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TopDownController : MonoBehaviour
{

    public Rigidbody2D body;
    public SpriteRenderer spriteRenderer;
    public List<Sprite> nSprites;
    public List<Sprite> neSprites;
    public List<Sprite> seSprites;
    public List<Sprite> sSprites;
    public List<Sprite> eSprites;
    public float walkSpeed;
    public float frameRate;
    float idleTime;
    public Vector2 direction;
    public bool doesPlayerPossess;
    public bool playerInRange;
    Vector2 offset;
    public List<Sprite> selectedSprites = null;

    // Start is called before the first frame update
    void Start()
    {
        float offsetX = offset.x;
        float offsetY = offset.y;

    }

    // Update is called once per frame
    void Update()
    {
        direction = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).normalized;
       
        body.velocity = direction * walkSpeed;

        HandleSpriteflip();

        List<Sprite> directionSprites = GetSpriteDirection();
       

        if(directionSprites != null)
        {
            //holding inputs
            float playTime = Time.time - idleTime;
            int frame = (int)((playTime * frameRate) % directionSprites.Count);
            spriteRenderer.sprite = directionSprites[frame];
        }
        else
        {
            //holding no inputs
            idleTime = Time.time;
        }
    }

    void HandleSpriteflip()
    {
        if (!spriteRenderer.flipX && direction.x < 0)
        {
            spriteRenderer.flipX = true;
        }
        else if (spriteRenderer.flipX && direction.x > 0)
        {
            spriteRenderer.flipX = false;
        }

    }

    public List<Sprite> GetSpriteDirection()
    {

        //north
        if(direction.y > 0)
        {
            if(Mathf.Abs(direction.x) > 0) //east or west
            {
                selectedSprites = neSprites;
            }
            //neutral x value
            else
            {
                selectedSprites = nSprites;
            }
        }
        //south
        else if(direction.y < 0)
        {
            if (Mathf.Abs(direction.x) > 0) //east or west
            {
                selectedSprites = seSprites;
            }
            //neutral x value
            else
            {
                selectedSprites = sSprites;
            }
        }
        //neutral
        else
        {
            if(Mathf.Abs(direction.x) > 0)
            {
                selectedSprites = eSprites;
            }
        }

        return selectedSprites;
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.CompareTag("Crate"))
        {
            playerInRange = true;
        }
    }

    void OnCollisionExit2D(Collision2D col)
    {
        if (col.gameObject.CompareTag("Crate"))
        {
            playerInRange = false;
        }
    }
}

My attempt to copy the image onto the message failed, but it should be right below it under the attached files segment.

Not really, because line 56 is just a curly bracket. If you click on the error message does it give you an actual stack trace?

I think the error message has something to do with an indexer, so I would think the error might have something to do with this line here:

spriteRenderer.sprite = directionSprites[frame];

Use Debug.Log to see the value of frame and the value of directionSprites.Count when the error occurs.

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

Steps to success:

  • find which collection it is and what line of code accesses it <— critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection size
  • remember you might have more than one instance of this script in your scene/prefab
  • remember the collection may be used in more than one location in the code
  • remember that indices start at ZERO (0) and go to the count / length minus 1 (three (3) items are 0,1,2 only)