I am trying to create a Medic type unit in a RTS prototype, and am now stuck when my Medic tries to heal other units.
Every 10 seconds, the Medic creates an array of every other friendly unit, then tries to add 100 to their health script, finally the array is destroyed. I am able to create/delete the array no problem, but am currently unable to access the health component of each GameObject.
The error occurs at line 39, as enemyHeal… is invalid. I’m not sure what should be in its place.
public float timer;
private float timerMax;
public GameObject[] enemyHeal;
void Start()
{
timer = 0.0f;
timerMax = 10f;
}
void Update()
{
timer += Time.deltaTime;
if(timer > timerMax)
{
timer = 0.0f;
FindHealUnits();
}
}
void FindHealUnits()
{
GameObject[] enemyHealGameObjectReference = GameObject.FindGameObjectsWithTag("Enemy");
enemyHeal = new GameObject[enemyHealGameObjectReference.Length];
for (int i = 0; i < enemyHealGameObjectReference.Length; i++)
{
enemyHeal[i] = enemyHealGameObjectReference[i];
}
if(enemyHeal != null)
{
StartCoroutine(HealUnits());
}
Debug.Log("Found nearby units");
}
IEnumerator HealUnits()
{
var heal = enemyHeal[i].GetComponent<EnemyHealth>();
heal.health = heal.health + 100;
//Heal the units
//Do a snazzy effect/particle and SFX
yield return new WaitForSeconds(0.5f);
enemyHeal = null;
Debug.Log("Healed units and reset array");
}
If any more information is required to make things clearer, I am happy to provide it. Any help would be appreciated.