private void OnTriggerExit(Collider other) { // add the appropriate type here // Cache IHappinessIncreaseable so we don't have to get it twice IHappinessIncreaseable visitor = (IHappinessIncreaseable)other.GetComponent(typeof(IHappinessIncreaseable)); if (visitor != null) { visitors.Remove(visitor); // There should never be both an IHappinessIncreaseable and ICreature, correct? // Just return here or put an else on next if. } // Cache ICreature so we don't have to get it twice ICreature creature = (ICreature)other.GetComponent(typeof(ICreature)); if (creature != null) { creatures.Remove(creature); } }
void GrabCreaturesAndVisitors(Vector3 center) { Collider[] visitorColliders = Physics.OverlapSphere(center, NodeAoe, layerMask); foreach (Collider hit in visitorColliders) { // Cache IHappinessIncreaseable so we don't have to get it twice IHappinessIncreaseable visitor = (IHappinessIncreaseable)hit.GetComponent(typeof(IHappinessIncreaseable)); if (visitor != null) { visitors.Add(visitor); } else { // Cache ICreature so we don't have to get it twice ICreature creature = (ICreature)hit.GetComponent(typeof(ICreature)); if (creature != null) { creatures.Add(creature); } } } }