코루틴

CJB_ny·2022년 9월 26일
0

Unity_Tip

목록 보기
6/6
post-thumbnail

https://wergia.tistory.com/167


public Action<GameObject, Define.WorldObject, bool> OnItemEffectEvent;


 private void OnItemEffect(GameObject go, Define.WorldObject type, bool on)
    {
        switch (type)
        {
            case Define.WorldObject.ItemEffect:
                {
                    GameObject obj = Managers.Game.Spawn(Define.WorldObject.ItemEffect, "Effect/ItemGetEffect");

                    obj.transform.position = go.transform.position;
                    Managers.Game.Despawn(go);
                    LinkCoroutine(obj, Define.WorldObject.ItemEffect);
                }
                break;
        }
    }

    void LinkCoroutine(GameObject go, Define.WorldObject type)
    {
        StartCoroutine(DespawnItemEffect(go, type));
    }

    IEnumerator DespawnItemEffect(GameObject go, Define.WorldObject type)
    {
        if (type == Define.WorldObject.ItemEffect)
        {
            yield return new WaitForSeconds(1.0f);
            Managers.Game.Despawn(go);

            yield return null;
        }
    }

현재 OnItemEffectEvent 이벤트에다가 이벤트 등록한뒤에 코루틴을 작성했는데

참조에러가 나서 뭔가 싶었다...

구독을 해준 OnItemEffect함수가 switch문을 만나고 코루틴 실행을 한뒤

break를 만나서 스택에서 사라지니, 다시 코루틴을 호출한 함수를 통해서 접근을 할려고하다보니 스택에서 메모리가 날아가서 발생하는 문제인가 싶었는데

이것은 아니고

참고 블로그를 보면은 해당 오브젝트가 삭제가 되면은 (비활성화) 되어있다면 코루틴이 호출되지 않는다.

내가 Despawn 으로 비화성화 처리를 했기 때문에...

profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글