Child and foreach ?
Hello, I have two different problems. I am making a tactil game where each enemy have a symbol. When you execute it you kill the closest. The problem is when I kill one, they all die (all already instanciate). Maybe it's a problem with the foreach. Then, I don't know how to target by symbol (currently the symbol are just child of the prefab.
GameObject GestureAttack() { Invincible();
GameObject[] Foes = GameObject.FindGameObjectsWithTag("Foe");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
Transform Zed = transform.Find("UI_zed");
foreach (GameObject go in Foes)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
closest2 = new Vector3(closest.transform.position.x-1, 1, closest.transform.position.z-1);
}
}
if (recupgeste == "GAcircle" && recupgestescore > 0.80 || Input.GetKeyDown(KeyCode.Space))
{
transform.position = Vector3.MoveTowards(transform.position, closest2, 50 /*100*Time.deltaTime*/);
foreach (GameObject go in Foes)
{
FoeCurrentHealth = go.GetComponent<s_EnnemyControl>().FoeCurrentHealth;
s_EnnemyControl enemyHealth = go.GetComponent<s_EnnemyControl>();
if (FoeCurrentHealth >0)
{
// Debug.Log("TOOK");
enemyHealth.TakeDamage(attackDamage);
}
}
}
return closest;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612080407im_/https://answers.unity.com/themes/thub/images/avi.jpg)