2014. 9. 28.

OnTriggerEnter2D and OnTriggerExit2D not working?

유니티를 쓰던 도중, 충돌 체크가 필요해서 OnTriggerEnter2D와 OnTriggerExit2D를 선언했다.
using UnityEngine;
using System.Collections;

public class EnemyGuideWatcher : MonoBehaviour
{
  public EnemyControllerScript enemyObject = null;
  public bool onCollided = false;

  void onTriggerEnter2D (Collider2D collidedObject)
  {    
    onCollided = true;
  }
 
  void onTriggerExit2D (Collider2D collidedObject)
  {
    onCollided = false;
    // Is this trigger just left a Platform object, then the enemy is about to walk off the platform.
    // Tell the enemy that they need to switch directions!
    if (collidedObject.tag == "Platform") {
      enemyObject.switchDirections (); 
    }
  }
}
워낙 기초적이고 간단한 작업이라 뚝딱 완성하고 실행을 시켰더니 왠걸? 충돌 체크가 되지 않는 것이였다. 제대로 선언했나 확인도 해보고, 로그도 찍어보고, 유니티 포럼도 뒤져가면서 정보를 찾아보았으나 원인을 알 수 없었다. 너무나 기초적인 작업이라 버그일리도 없고, 논리적 오류가 발생했을리도 없었다. 도대체 왜 안되는거야?!

3시간 후, onCollided의 값이 true로 바뀌길 바라며 여기저기 포럼 글을 헤매다, 아래와 같은 질문글을 발견했다.

The function isn't working because the letter "o" is lowercase instead of uppercase.
Change this line: void onTriggerExit2D(Collider2D other)
to: void OnTriggerExit2D(Collider2D other)
대소문자 문제였다는 간단한 사실을 3시간 동안 깨닫지 못했다니... 이런건 IDE에서 자동으로 색상을 통해 알려줘야 하는거 아냐? 모노디벨롭에 아쉬움이 느껴지는 순간이였다.
결론은 OnTrigger 함수가 제대로 작동하지 않는다면, 첫 글자를 대문자로 선언했는지 다시 한 번 확인해보자!

댓글 1개:

  1. 와... 혼자 맨땅에 헤딩식으로 공부하던 중이라 이틀째 대체 뭐가 문젠지 고민했는데 저는 OnTrigger2d라고 썻더라구요... 2D인데...하... 덕분에 해결했습니다. 정말 감사해요ㅠㅠㅠㅠ

    답글삭제