현재 상황 : 스킬을 사용하면 대상 지역에 있는 몬스터들을 해시셋에 싹 담아놓고 일정 시간뒤에 그 해시셋에 있는 몬스터들에게 데미지를 줌

문제 : 그 데미지를 받아서 몬스터가 파괴되어도 여전히 해시셋에 있는 몬스터에 참조하려다가 에러가 남 

내가 생각한 해결방안 : 몬스터 사망이벤트 만들어서 해시셋에 처음에 등록할때 이벤트에도 함수구독해서 파괴되면 해시셋에서 삭제하기. 

궁금한점 : 저 방법이 괜찮은 방법인지 모르겠음. 그냥 내가 떠오른게 저거뿐이긴함.. 



해당 클래스 다른 자잘한 내용들 다 빼고 딱 질문과 관련된 부분만 옮겨서 적을게요.


public class SkillObject : MonoBehaviour

{

    // 이 오브젝트와 부딪힌 오브젝트들 저장 (사이클마다 검사)

    private HashSet<Monster> collidingObjects = new HashSet<Monster>();


    private bool isApplicable => (currentApplyCount < applyCount)

        && (currentApplyCycle >= applyCycle);


    private void Update()

    {

        if (isApplicable)

            Apply();  -----------> Update에서 시간지날때마다 Apply를 시도

    }


    private void Apply()

    {

        foreach (Monster monster in collidingObjects)

        {

            monster.EffectSystem.Apply(skill);   -----------> 현재 해시셋 순회하면서 하나하나 Apply 해주기

        }

    }


    private void OnTriggerEnter(Collider other)

    {

        Monster monster = other.GetComponent<Monster>();

        if (monster != null)

            collidingObjects.Add(monster);

    }

    private void OnTriggerExit(Collider other)

    {

        Monster monster = other.GetComponent<Monster>();

        if (monster != null)

            collidingObjects.Remove(monster);

    }

}