Hello i’m having a problem with the enemyHealth list inside the method SearchLowestEnemy. Basically, when i start the game the enemies array is filled with all the enemies in the scene. Then if they have a script called ExplosiveEnemyController (and they have without doubts) we take the health value from that script and add it to the list. Then it should print the list, but many errors occurs in unity, like if the list is empty but it shouldn’t. Here is the code:
using Boo.Lang;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.AI;
public class HealerEnemyController : MonoBehaviour
{
private NavMeshAgent navMeshAgent;
[SerializeField] private GameObject currentEnemy;
[SerializeField] GameObject[] enemies;
[SerializeField] List<int> enemyHealth;
[Header("Heal settings")]
[SerializeField] private float healRate, nextTimeToHeal;
[SerializeField] private int healQuantity;
private void Awake()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
private void Start()
{
InvokeRepeating("SearchLowestEnemy", 0, 15);
}
private void Update()
{
Heal();
}
private void SearchLowestEnemy()
{
enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject enemy in enemies)
{
if (enemy.GetComponent<ExplosiveEnemyController>())
enemyHealth.Add(enemy.GetComponent<ExplosiveEnemyController>().health);
else
continue;
}
Debug.Log(enemyHealth);
if (enemyHealth.Count != 0)
{
enemyHealth = enemyHealth.Sort();
for (int i = 0; i < enemyHealth.Count; i++)
{
if (enemyHealth.ToArray()[0] == enemies*.GetComponent<ExplosiveEnemyController>().health)*
{
Debug.Log("Gameobject found: " + enemies*.name);*
currentEnemy = enemies*.gameObject;*
return;
}
else
continue;
}
}
}
private void Heal()
{
navMeshAgent.SetDestination(currentEnemy.transform.position);
navMeshAgent.stoppingDistance = 8;
if (healRate >= nextTimeToHeal)
{
currentEnemy.gameObject.GetComponent().health += healQuantity;
healRate = 0;
}
else
healRate += Time.deltaTime;
if (currentEnemy.GetComponent().health >= currentEnemy.GetComponent().maxHealth)
{
return;
}
}
}