2015-08-16 70 views
-2

我的腳本...爲什麼我一直得到這個NullReferenceException錯誤?

using UnityEngine; 
using System.Collections; 

public class CountDownTimer : MonoBehaviour { 

    public int score; 
    float timeRemaining = 15; 

    // Use this for initialization 
    void Start() { 
    } 

    // Update is called once per frame 
    void Update() { 
     timeRemaining -= Time.deltaTime; 
     score = GetComponent<TriggerZone>().score; 
    } 

    void OnGUI(){ 
     if (timeRemaining > 0) { 
      GUI.Label(new Rect(100, 100, 200, 100), "Time Remaining: "+(int)timeRemaining); 
     } 
     else{ 
      GUI.Label(new Rect(100, 100, 200, 100), "Times up your score was: " + score + ". Press the r button to restart, or ESC to quit."); 
      if (Input.GetKeyDown("r")) 
       Application.LoadLevel("Testing Grounds"); 
      if (Input.GetKey("escape")) 
       Application.Quit(); 
     } 
    } 
} 

錯誤:

NullReferenceException: Object reference not set to an instance of an object CountDownTimer.Update() (at Assets/Scripts/CountDownTimer.cs:16)

我想不出什麼可能會造成這個錯誤。據我瞭解,這是試圖告訴我,一些東西不存在,但我想不出可能是什麼。 「score = GetComponent()。score;」正在訪問包含分數值的另一個腳本,以便當計時器用完時它會告知玩家他們的分數,並讓他們選擇退出或重新開始遊戲。

此外,如果它幫助這裏是其它腳本...

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

公共類TriggerZone:MonoBehaviour {

public Text MyText; 
public int score; 

// Use this for initialization 
void Start() { 

    MyText.text = ""; 

} 


// Update is called once per frame 
void Update() { 

    MyText.text = "$" + score; 

} 

void OnTriggerEnter(Collider coll) { 

    if (coll.gameObject.HasTag ("ValueLevel1")) 
     score = score + 5; 

    if (coll.gameObject.HasTag ("ValueLevel2")) 
     score = score + 25; 

    if (coll.gameObject.HasTag ("ValueLevel3")) 
     score = score + 50; 

    if (coll.gameObject.HasTag ("ValueLevel4")) 
     score = score + 100; 

    if (coll.CompareTag ("Pickable")) { 
     coll.gameObject.SetActive(false); 
    } 

} 

}

+0

這聽起來像'GetComponent ()'是'返回null',而是因爲你沒有足夠的發佈代碼爲我們重現您的問題,我們不能肯定。 – Enigmativity

+0

你還需要什麼?另一個腳本? – Sie

回答

0

嗯,我覺得自己很蠢,但多虧了Reddit我終於找到了問題。原來,我把我的腳本附加到我的FPS控制器,這是造成null問題。

0
GetComponent<TriggerZone>() 

似乎返回null和您正嘗試訪問.score關閉null。您的解決方案是檢查null的回報,並採取適當的措施或確保呼叫永不返回null

+0

這就是我迷失的地方。我使用「score = GetComponent ().score;」從TriggerZone獲取分數值。沒有它,它不起作用。所以它不能返回null。如果我沒有意義,我對C#相對陌生,所以很抱歉。 – Sie

+0

檢查兩個腳本(即TriggerZone和CountDownTimer)是否連接到同一個GameObject。根據你的腳本,他們必須是。如果你需要它們在單獨的遊戲對象上,那麼改變GetComponent行來讀取類似score = REFERENCE_TO_OTHER_GO.GetComponent ().score; –

+0

它們都連接到同一個GameObject(如你所說的那樣)。我不知道爲什麼這不起作用,因爲遊戲似乎運行良好。我很想嘗試開發,但我寧願先擠壓這個bug。 – Sie

0

不論出於什麼原因,你可能會錯過你的CountDownTimer實例的TriggerZone腳本/組件,請嘗試以下屬性:

[RequireComponent (typeof (TriggerZone))] //Try add this 
public class CountDownTimer : MonoBehaviour { 
... 
} 

這RequireComponent屬性可以讓你自動添加所需的組件作爲一個依賴。下面是Unity3D官方文檔:

When you add a script which uses RequireComponent, the required component will automatically be added to the game object. This is useful to avoid setup errors. For example a script might require that a rigid body is always added to the same game object. Using RequireComponent this will be done automatically, thus you can never get the setup wrong.

BTW:以下調用應該不會在更新()調用,最好在開始()或甲戌():

score = GetComponent<TriggerZone>().score; 
+0

即使添加了RequireComponent事件,也是如此。錯誤剛剛移動到第12行(在Start()中)。 「NullReferenceException:未將對象引用設置爲對象CountDownTimer.Start()(在Assets/Scripts/CountDownTimer中)的實例。cs:12)「 – Sie

+0

哪一行是12?你能否在你的文章中添加註釋以顯示第12行? – David

+0

以下是新代碼:https://ideone.com/qQAipj。 – Sie