簡體   English   中英

UNITY C#GetComponent返回Null

[英]UNITY C# GetComponent Returns Null

我最近決定研究使用C#在Unity中進行編程。 我遇到了這個討厭的問題,我不知道如何解決。 當我調用“ instanceGo.GetComponent”時,它幾乎返回null。 我不知道該怎么做。 任何想法都會有所幫助!

    // 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”。 告訴我是否需要查看更多代碼。

謝謝。

確保instanceGo確實已附加了所需的組件。

檢查instanceGo上是否有一個名為SpawnZombiesHandler的組件,並且您在正確的位置獲得了大寫字母。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM