无法将 "neighbours" 个特定图块加载到列表中
Having trouble loading "neighbours" of a specific tile into a List
这是我第一次在这里提问,实际上我的脚本有问题:
我正在尝试将图块的邻居添加到列表中,但在尝试打印列表中的内容时,我不断收到 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 (GameObject obj in ourHitObject.GetComponent().neighbours) {
打印("Name: " + obj.name + "Atual: " + i++);
}
已解决
@AnthonyLeal I looked at the code and that is the problem. You are searching for GameObject while do they don't exist. Just think about it like this: Your generated a gameObject in your for loop then called the populateNeighbours() function in that for loop. That populateNeighbours() function does not operate on the generated GameObject in that loop. It looks for other GameObjects that are not yet generated. You did this many many times leading to over 500 errors. – Programmer
The problem is probably in the code itself, when i try to add the
object to the list, reading it should be fine, for example If i add to
the list 6x"Left ones", ( x-1) ( y), it will run correctly. If i try
to add any other ( right, top, bottom ) the error is thrown when
printing.
发生这种情况是因为第一个 GameObject.Find("Tile_" + (x-1) + "_" + y)
在场景中找到了游戏对象。 GameObject.Find
方法的其余部分失败。当它失败时,它 returns null
。 null
被添加到 List
,即使那里什么也没有。
这是没有人可以为您解决的问题,因为我们在您的场景中看不到 GameObjects
的名称。您必须自己修复它,方法是在将其添加到 List
之前检查 GameObject.Find
returns null
。将您的 populateNeighbours()
函数替换为以下函数以轻松调试您的代码。如果它是 null
,它不会添加 GaeObject
,它会打印找不到的 GameObject
的名称。然后您可以使用它来检查 GameObject
是否确实存在于场景中。
此外,不要使用foreach
在Unity中循环List。请改用 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;
}
这是我第一次在这里提问,实际上我的脚本有问题: 我正在尝试将图块的邻居添加到列表中,但在尝试打印列表中的内容时,我不断收到 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 (GameObject obj in ourHitObject.GetComponent().neighbours) { 打印("Name: " + obj.name + "Atual: " + i++); }
已解决
@AnthonyLeal I looked at the code and that is the problem. You are searching for GameObject while do they don't exist. Just think about it like this: Your generated a gameObject in your for loop then called the populateNeighbours() function in that for loop. That populateNeighbours() function does not operate on the generated GameObject in that loop. It looks for other GameObjects that are not yet generated. You did this many many times leading to over 500 errors. – Programmer
The problem is probably in the code itself, when i try to add the object to the list, reading it should be fine, for example If i add to the list 6x"Left ones", ( x-1) ( y), it will run correctly. If i try to add any other ( right, top, bottom ) the error is thrown when printing.
发生这种情况是因为第一个 GameObject.Find("Tile_" + (x-1) + "_" + y)
在场景中找到了游戏对象。 GameObject.Find
方法的其余部分失败。当它失败时,它 returns null
。 null
被添加到 List
,即使那里什么也没有。
这是没有人可以为您解决的问题,因为我们在您的场景中看不到 GameObjects
的名称。您必须自己修复它,方法是在将其添加到 List
之前检查 GameObject.Find
returns null
。将您的 populateNeighbours()
函数替换为以下函数以轻松调试您的代码。如果它是 null
,它不会添加 GaeObject
,它会打印找不到的 GameObject
的名称。然后您可以使用它来检查 GameObject
是否确实存在于场景中。
此外,不要使用foreach
在Unity中循环List。请改用 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;
}