2016-07-13 2 views
0

최근에 C#을 사용하여 Unity에서 프로그래밍을 살펴보기로 결정했습니다. 나는이 불쾌한 문제에 부 닥쳤다. 나는 그것을 고치는 법을 모른다. 꽤 많이 "instanceGo.GetComponent"를 호출하면 null을 반환합니다. 나는 무엇을해야할지 모른다. 어떤 아이디어라도 도움이 될 것입니다!UNITY C# GetComponent Returns Null

CODE

// Update is called once per frame 
void Update() { 
    if (Input.GetKey ("p")) { 
     printMessage = !printMessage; 
    } 
    if (instanceGo.GetComponent<SpawnZombiesHandler>() == null) { 
     print ("1"); 
    } else { 
     print ("2"); 
    } 
    foreach(GameObject bullet in instanceGo.GetComponent<ArcherBulletShootHandler>().getBullets()){ 
     print ("12"); 
     foreach (GameObject zombie in instanceGo.GetComponent<SpawnZombiesHandler>().getZombies()) { 
      hit (bullet, zombie); 
      if(Mathf.RoundToInt(zombie.transform.position.y) == Mathf.RoundToInt(bullet.transform.position.y) && 
       Mathf.RoundToInt(zombie.transform.position.x) == Mathf.RoundToInt(bullet.transform.position.x)) { 
        print("HIT!"); 
       } 
     } 
    } 
} 

참고 그것은합니까 인쇄 "1". 더 이상 코드가 필요한지 알려주십시오.

감사합니다.

+0

'C#'과 'C'는 완전히 다른 언어입니다. – kaylum

+0

@kaylum 이것은 C# – Nicster15

+0

입니다. 나는 그것이'C#'임을 압니다. 하지만 원래는'C' 태그를 사용하여 누군가 당신을 위해 친절하게 제거했습니다. – kaylum

답변

0

instanceGo가 실제로 원하는 구성 요소를 첨부했는지 확인하십시오.

+0

명확하게 말씀 해주시겠습니까? – Nicster15

+1

개체를 선택하면 Inspector 탭에서 첨부 된 모든 구성 요소를 볼 수 있습니다. instanceGo에 원하는 구성 요소가 있는지 확인하십시오 (예 : SpawnZombiesHandler). –

0

instanceGo에 연결된 SpawnZombiesHandler라는 구성 요소가 있고 올바른 위치에 대문자가 있는지 확인하십시오.