2016-06-21 64 views
1

這是我第一次在這裏提問,實際上我的腳本存在問題: 我試圖將瓷磚的鄰居添加到列表,但我不斷收到NullReferenceException:在嘗試打印列表中的內容時,未將對象引用設置爲對象的實例。無法將特定瓷磚的「鄰居」加載到列表中

要注意第一個「list.Add」實際上是工作的,其他的不是。 爲了測試它,我總是試圖找到存在的東西,所有Tile_(x)_(y)都存在。

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 

public class TileStats : MonoBehaviour { 

    public int x; 
    public int y; 
    public List<GameObject> neighbours; 

    public List<GameObject> populateNeighbours() 
    { 
     List<GameObject> list = new List<GameObject>(); 
     //If we're at x, y. 
     //Left one is at x-1,y 
     list.Add(GameObject.Find("Tile_" + (x-1) + "_" + y)); 

     //Right one is at x+1,y 
     list.Add(GameObject.Find("Tile_" + (x+1) + "_" + y)); 


     //Bottom ones are at x,y-1 and x+1,y-1 
     list.Add(GameObject.Find("Tile_" + x + "_" + (y-1))); 
     list.Add(GameObject.Find("Tile_" + (x+1) + "_" + (y-1))); 

     //Top ones are at x,y+1 and x+1,y+1 
     list.Add(GameObject.Find("Tile_" + x + "_" + (y+1))); 
     list.Add(GameObject.Find("Tile_" + (x+1) + "_" + (y+1))); 
     return list; 

    } 
} 


... 
Tile_GO.GetComponent<TileStats>().x = x; 
Tile_GO.GetComponent<TileStats>().y = y; 
Tile_GO.GetComponent<TileStats>().neighbours=Tile_GO.GetComponent<TileStats>().populateNeighbours(); 
... 

當我嘗試打印輸入錯誤被拋出: 的foreach(遊戲對象OBJ在ourHitObject.GetComponent()的鄰居。){ 打印( 「名稱:」 + obj.name + 「Atual:」 + i ++); }

解決

@AnthonyLeal我看了看代碼,這就是問題。您正在搜索GameObject,而它們不存在。試想一下:你在for循環中生成一個gameObject,然後在for循環中調用populateNeighbours()函數。 populateNeighbours()函數不會在該循環中生成的GameObject上運行。它會查找尚未生成的其他GameObjects。你做過這麼多次導致超過500個錯誤。 - 程序員

+0

的可能的複製[「對象未設置」錯誤(http://stackoverflow.com/questions/3487637/object-reference-not-set-error) – Henders

+0

你不顯示你所有的代碼,所以沒有人可以幫助你。請注意,很簡單,你不會在任何地方設置「鄰居」。你的意思是把它設置在「人口稠密」嗎? – Fattie

+0

請注意,populateNeighbours **必須**被稱爲PopulateNeighbours,不能填充Neighbours。你需要改變它並編輯代碼來反映它。 – Fattie

回答

0

這個問題可能是在代碼本身,當我嘗試了 對象添加到列表中,閱讀它要細,例如,如果我加入 列表6X「左者「,(x-1)(y),它會正確運行。如果我嘗試 添加任何其他(右,上,下),打印 時會引發錯誤。

發生這種情況是因爲第一個GameObject.Find("Tile_" + (x-1) + "_" + y)在場景中找到了GameObject。 GameObject.Find方法的其餘部分失敗。當它失敗時,它將返回null。那null被添加到List即使沒有什麼。

這是沒有人可以爲您解決的,因爲我們無法在您的場景中看到GameObjects的名稱。您必須通過檢查GameObject.Find返回null,然後將其添加到List之前自行解決。使用下面的功能替換您的populateNeighbours()函數,以輕鬆調試您的代碼。如果它是null,它將不會添加GaeObject,它將打印它找不到的名稱GameObject。然後,您可以使用它來檢查該場景中是否存在GameObject

另外,不要使用foreach來循環在統一列表。改爲使用for循環。

public List<GameObject> populateNeighbours() 
{ 
    List<GameObject> list = new List<GameObject>(); 
    //If we're at x, y. 
    //Left one is at x-1,y 
    GameObject tempObj = GameObject.Find("Tile_" + (x - 1) + "_" + y); 
    if (tempObj == null) 
     Debug.Log("Tile_" + (x - 1) + "_" + y + " Does NOT exist"); 
    else 
     list.Add(tempObj); 

    //Right one is at x+1,y 
    tempObj = GameObject.Find("Tile_" + (x + 1) + "_" + y); 
    if (tempObj == null) 
     Debug.Log("Tile_" + (x + 1) + "_" + y + " Does NOT exist"); 
    else 
     list.Add(tempObj); 

    //Bottom ones are at x,y-1 and x+1,y-1 
    tempObj = GameObject.Find("Tile_" + x + "_" + (y - 1)); 
    if (tempObj == null) 
     Debug.Log("Tile_" + x + "_" + (y - 1) + " Does NOT exist"); 
    else 
     list.Add(tempObj); 

    tempObj = GameObject.Find("Tile_" + (x + 1) + "_" + (y - 1)); 
    if (tempObj == null) 
     Debug.Log("Tile_" + (x + 1) + "_" + (y - 1) + " Does NOT exist"); 
    else 
     list.Add(tempObj); 

    //Top ones are at x,y+1 and x+1,y+1 
    tempObj = GameObject.Find("Tile_" + x + "_" + (y + 1)); 
    if (tempObj == null) 
     Debug.Log("Tile_" + x + "_" + (y + 1) + " Does NOT exist"); 
    else 
     list.Add(tempObj); 

    tempObj = GameObject.Find("Tile_" + (x + 1) + "_" + (y + 1)); 
    if (tempObj == null) 
     Debug.Log("Tile_" + (x + 1) + "_" + (y + 1) + " Does NOT exist"); 
    else 
     list.Add(tempObj); 

    return list; 
} 
+0

感謝您的回答,我再也沒有收到錯誤了。儘管如此,它仍然無法正常工作。我沒有得到我的鄰居:我會嘗試解釋 - 我在鼠標事件上「打印」鄰居(只是爲了測試它在我用em做其他事情之前是否工作)。在日誌中,使用您的代碼說明Tile_2_1不存在。但是,如果我點擊圖塊3_1,它將'打印'Tile_2_1作爲鄰居,因此它存在。只有左邊的一個正在打印。 每當創建一個貼圖時,我都會調用這個函數,它幾乎每個貼圖都不存在垃圾信息Tile_x_y。 –

+0

@AnthonyLeal當然,錯誤應該消失,因爲它不再向列表中添加空對象。下一部分是讓您檢查並確保名稱與您正在打印的內容相符。 「**但是如果我點擊圖塊3_1,它會'打印'Tile_2_1,所以它存在。**」不要點擊任何東西。在Scene Hierarchy中查看它是否存在。另外,您是在運行時生成拼貼還是在點擊播放之前已經有拼貼? – Programmer

+0

是的,瓷磚在那裏,在場景中我有11x11瓷磚命名。他們在層次結構中。它從Tile_0_0到Tile_10_10。名字是正確的,它可能是愚蠢的,但我找不到解決方案。 –