Hello guys,
I was trying to create a method for attacking based on a Brackeys video I saw, but I wanted to use more than just one single circle to check the collision so I tried to use an array of of transforms and another array for the range on each circle, but I keep getting two error messages that i can’t get rid off (
IndexOutOfRangeException: Index was outside the bounds of the array.
NullReferenceException: Object reference not set to an instance of an object
espadaencantada.OnDrawGizmosSelected ()
)
here’s the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class espadaencantada : itens
{
float[ ] attackRange;
public Transform[ ] transforms;
public LayerMask Enemies;
public int health;
private int i = 0;
private void Awake ()
{
attackRange = new float[5];
attackRange[0] = 0.5f;
attackRange[1] = 0.7f;
attackRange[2] = 1f;
attackRange[3] = 0.7f;
attackRange[4] = 0.5f;
transforms = new Transform[5];
}
public override void ataque(int attack)
{
foreach(Transform transform in transforms)
{
Collider2D[ ] hitenemies = Physics2D.OverlapCircleAll(transform.position, attackRange*, Enemies);*
i++;
foreach(Collider2D inimigos in hitenemies)
{
health -= attack;
}
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
ataque(attack);
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
foreach (Transform transform in transforms)
{
Gizmos.DrawWireSphere(transform.position, attackRange*);*
}
}
}
I’m still a begginer at coding and I don’t know a way to acces each element on the attackRange array using a foreach
And the attack method is in the other itens script, but there is nothing on it there