/** * function to check for the collisions with the bullet */ protected override void CheckCollisions() { if (Collider.CollidesWith(Target)) // if the bullet collides with a target { Destroy(gameObject); // destroy the bullet // get the information on the nutrient target that was hit Nutrient target = Target.GetComponent <Nutrient>(); Color targetColor = target.BodyColor; GameObject parent = Target.transform.parent.gameObject; target.OnBulletCollision(); // call the function on the target to handle a collision targets--; // decrement the targets counter Nutrient[] nutrients = parent.GetComponentsInChildren <Nutrient>(); // get other nutrients with the same parent // as the original target // iterate over the nutrients to see if there are any other valid targets with the same color if the bullet // is able to destroy more targets foreach (Nutrient nutrient in nutrients) { if (nutrient.BodyColor == targetColor && targets > 0 && !nutrient.IsTargetted) { targets--; nutrient.OnBulletCollision(); } } } }