I’m getting the index was out of range error whenever the current target is offscreen because of this script and I’m not sure why. Can someone out there please tell me how to fix this? Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class targetingSystemV2 : MonoBehaviour
{
public GameObject currentTarget;
public int currentTargetId;
public List<GameObject> listOfTargets = new List<GameObject>();
void Update()
{
if(listOfTargets.Count > 0 && listOfTargets[currentTargetId] != null)
{
currentTarget = listOfTargets[currentTargetId];
}
else
{
currentTarget = null;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "Target")
{
listOfTargets.Add(col.gameObject);
}
}
void OnTriggerExit2D(Collider2D col)
{
listOfTargets.Remove(col.gameObject);
}
}