2013-06-30 43 views
0

我得到這個錯誤在Unity:的NullReferenceException不能找出什麼是空

的NullReferenceException:未設置爲一個對象 TowerSlot.OnGUI(實例對象引用)(在資產/ TowerSlot.cs:26)

我是相對較新的統一,無法弄清楚這個錯誤是從哪裏來的(我假設26),我不知道什麼是空的。如果有人可以請幫助向我解釋如何理解錯誤指向什麼以及我需要做什麼,我將非常感謝。

TowerSlot.cs:

using UnityEngine; 
using System.Collections; 

public class TowerSlot : MonoBehaviour { 
    public GUISkin skin = null; 

    bool gui = false; 

    // Tower prefab 
    public Tower towerPrefab = null; 

    void OnGUI() {  
     if (gui) { 
      GUI.skin = skin; 

      // get 3d position on screen   
      Vector3 v = Camera.main.WorldToScreenPoint(transform.position); 

      // convert to gui coordinates 
      v = new Vector2(v.x, Screen.height - v.y); 

      // creation menu for tower 
      int width = 200; 
      int height = 40; 
      Rect r = new Rect(v.x - width/2, v.y - height/2, width, height); 
      GUI.contentColor = (Player.gold >= towerPrefab.buildPrice ? Color.green : Color.red); 
      GUI.Box(r, "Build " + towerPrefab.name + "(" + towerPrefab.buildPrice + " gold)"); 

      // mouse not down anymore and mouse over the box? then build the tower     
      if (Event.current.type == EventType.MouseUp && 
       r.Contains(Event.current.mousePosition) && 
       Player.gold >= towerPrefab.buildPrice) { 
       // decrease gold 
       Player.gold -= towerPrefab.buildPrice; 

       // instantiate 
       Instantiate(towerPrefab, transform.position, Quaternion.identity); 

       // disable gameobject 
       gameObject.SetActive(false); 
      } 
     } 
    } 

    public void OnMouseDown() { 
     gui = true; 
    } 


    public void OnMouseUp() { 
     gui = false; 
    } 
} 

此外,我想在這裏學習本教程http://makeagame.info/unity-tower-defense-game-step-4-scripting

謝謝!

回答

0

OP對@Hatchet的回覆中的屏幕截圖顯示塔架預製沒有在編輯器中設置。

要麼它應該由其他代碼設置(這是不工作),或者應該手動設置。您可以通過將塔架預製件拖放到插槽上或點擊'無(塔)'右邊的小圓圈來實現此目的:這將彈出一個選擇器對話框,您可以在其中選擇塔架預製件。它顯示爲「塔」,因爲這是它想要的變量。

順便說一句GUIskin也沒有設置。這可能會導致一個問題,當你打行統一的GUI分配這樣的引用

GUI.skin = skin; 

文檔是在這裏:http://docs.unity3d.com/Documentation/Manual/EditingReferenceProperties.html

+0

真棒,它修復它感謝。 – Ivatrix

2

您在早期將towerPrefab設置爲null,然後在第26行中,在爲其分配任何非空值之前,先引用其buildPrice屬性或字段。這將拋出一個空例外。

這條線的問題是:

GUI.contentColor = (Player.gold >= towerPrefab.buildPrice ? Color.green : Color.red); 
+0

塔公towerPrefab = null被創造統一的公共屬性。 OP可能忘記了在Unity編輯器中給它賦值 - 而不是在代碼中。 – theodox

+0

@theodox - 我看起來太簡單了,並且正在考慮塔式結構是該方法中的一個局部變量。我仍然認爲towerPrefab是空的。在執行該行之前,它不會設置爲非null。 – hatchet

+0

這是我在統一編輯器http://gyazo.com/90166d36a096ee0953d5f4f386d32c0c中看到的,如果我嘗試添加其他資源和場景顯示爲空白。我有沒有分配它?我不確定,因爲它在無括號後面寫着方括號中的預製件的名稱... – Ivatrix

0

前嫌所指出的問題。我想,這可能解決這個問題:

public Tower towerPrefab = new Tower(); 

因爲Tower類有您試圖訪問屬性buildPrice

+0

我嘗試過,得到了相同的空錯誤以及兩個警告:您嘗試使用'new'關鍵字創建MonoBehaviour。這是不允許的。 MonoBehaviours只能使用AddComponent()添加。或者,您的腳本可以繼承ScriptableObject或根本沒有基類 AND您嘗試使用'new'關鍵字創建MonoBehaviour。這是不允許的。MonoBehaviours只能使用AddComponent()添加。或者,您的腳本可以繼承ScriptableObject或根本沒有基類 UnityEngine.MonoBehaviour:.ctor() Tower:.ctor() TowerSlot:.ctor() – Ivatrix

相關問題