Question by
tsikerdekis · Oct 18, 2015 at 02:50 PM ·
listfor-loop
List.contains doesn't work and add duplicates
I am trying to avoid having to add duplicates in a list but for some reason the code doesn't work. What am I doing wrong? here is the code:
CharacterController characterController = GameObject.Find("Player").GetComponent<CharacterController>();
Vector3 pointOfOrigin = transform.position + characterController.center;
RaycastHit[] hit = Physics.SphereCastAll(pointOfOrigin, characterController.height / 2, transform.forward, 0.5f);
List<int> enemiesHit = new List<int>();
for (int i = 0; i < hit.Length; i++)
{
if (hit[i].transform.gameObject.tag == "Enemy" & !enemiesHit.Contains(hit[i].transform.gameObject.GetInstanceID()))
{
enemiesHit.Add(hit[i].transform.gameObject.GetInstanceID());
hit[i].transform.gameObject.GetComponent<HealthController>().TakeDamage(damage);
}
}
for (int x = 0; x < enemiesHit.Count; x++)
{
Debug.Log(enemiesHit[x]);
}
Comment
Answer by tsikerdekis · Oct 18, 2015 at 03:01 PM
Nevermind, it seems to work, it's just debug.log if placed within the loop won't display anything for some reason.