IndexOutOfRangeException?

Hey guys,

I have two characters in my mobile game so far, in my mobile game the player can smash boxes and kill enemies. I have a script attached to all of them that instantiates the loot drops.

This is that script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemDrop : MonoBehaviour
{
    [SerializeField]
    private GameObject[] itemList;
    private int itemNum;
    private int randNum;
    private Transform Epos;
 
    private void Start()
    {
       // itemList = CamFollow.itemListPass;
        Epos = GetComponent<Transform>();
        Debug.Log(itemList);
    }
    public void DropItem()
    {
     
       
            randNum = Random.Range(0, 100000); // 100% total for determining loot chance;
            Debug.Log("Random Number is " + randNum);
            if (randNum >= 100000) // Rainbow quality
            {
                itemNum = 5;
                Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);
            }
            if (randNum > 99001 && randNum <= 99999) // Epic quality
            {
                itemNum = 4;
                Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);
            }
            if (randNum > 90001 && randNum <= 99000) // Rare quality
            {
                itemNum = 3;
                Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);
            }
            else if (randNum > 55001 && randNum <= 90000) // Normal quality
            {
                itemNum = 2;
                Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);
            }
            else if (randNum > 30001 && randNum <= 55000)// Drops money
            {
                itemNum = 1;
                Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);
            }
            else if (randNum > 1 && randNum <= 30000)// Drops nothing
            {
                itemNum = 0;
                Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);
            }
     
    }
}

My issue is that it works great for my warrior, who is the melee character, but when I switch over and do the same thing with my ranged character, I get this error:

The error is happening on the Instantiate(itemList[itemNum], Epos.position, Quaternion.identity); lines of code.
I think this error is preventing the enemies from dying, the script rolls the number for their loot drop but I get this error and the enemies aren’t drying. Any ideas what might be causing this?

Thanks!

You commented out putting elements in to the itemList array. The array is empty.

I don’t understand what you mean. In my editor there’s more spaces. So line 55 is the Instantiate line of code, the same code works fine for one character but not the other.

At the first line in DropItem put Debug.Log($“itemList: {itemList.Length}”); and confirm if you get the array size.

Okay, I will do this and check it for both of my character classes.

I have not solved this issue but I have found out what’s happening.

I have this script for my melee character:

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

public class PlayerCombat : MonoBehaviour
{
   
    public Animator animator;
    public Transform attackPoint;
    public LayerMask enemyLayers;

    public float attackRange = 0.5f;
    public int attackDamage = 40;

    // Start is called before the first frame update
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        Attack();
    }

    // Update is called once per frame
    public void Attack()
    {
        animator.SetTrigger("Attack");
        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

        foreach(Collider2D enemy in hitEnemies)
        {
            enemy.GetComponent<Enemy>().TakeDamage(20);
            Debug.Log("We hit " + enemy.name);

        }
    }
    public void PlayerAttack()
    {
        if (Input.GetKeyDown("Attack"))
        Attack();
    }


    void OnDrawGizmosSelected()
    {
    if (attackPoint == null)
        return;
    Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    }
}

And these two scripts for my ranged character:

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

public class RangedWeapon : MonoBehaviour
{
    public Transform firePoint;
    public GameObject bulletPrefab;



    // Update is called once per frame
    public void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }

    public void Shoot()
    {
        Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunBullet : MonoBehaviour
{

    public float speed = 20f;
    public Rigidbody2D rb;
    public int damage = 40;

    private Enemy enemy;

    // Start is called before the first frame update
    void Start()
    {
        rb.velocity = transform.right * speed;
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Enemy")
        {
            enemy.GetComponent<Enemy>().TakeDamage(damage);
            Debug.Log("We hit " + enemy.name);

          
        }
       
       
       

       
    }
   
   


}

I have the attack button for my ranged set as the Shoot() function, which instantiates the bullet just fine but the enemies still aren’t dropping loot or dying. Then I tried to change it to the same function that my melee class is using the public void Attack() from the first script, and although my bullet is no longer being instantiated, they are dying correctly if I’m right on top of them like my melee class.

So it seems like I have to rework my ranged script for my bullet to work similar to the line of code in the public void Attack() function, but triggered from bullet collision and not melee.

Collider2D[ ] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

This is the line it seems I’d have to rework to work off of a prefab bullet and not an actual attack point, any suggestions?