试图引用数组中对象中的值,但失败
本文关键字:失败 对象 引用 数组 | 更新日期: 2023-09-27 17:52:11
我花了几天时间尝试不同的技术来获得两个X乘Z数组彼此交谈,并决定通过制作2乘X乘Z数组来重做这一切。0,x,z包含一个立方体网格(预制单元),它将存储稍后用于NPC导航的值,而1,x,z包含一个高长方体网格(预制ObstBlock),用于检测场景中的墙壁。如果有墙,ObstBlock的int受阻将变成1。我试图在主脚本中引用的就是这个int。
所有的cell和obstblock都是由运行GridScript的空对象(名为"Grid")生成的。所有的cell运行CellScript,所有的obstblock运行ObstBlockScript。以下是我在GridScript中的尝试:
void DetectObstructions(){
for (int x=0; x<GridSize.x; x++) {
for (int z=0; z<GridSize.z; z++) {
Transform cell;
Transform block;
cell = Grid [0, x, z];
block = Grid [1, x, z];
if (block.GetComponent<ObstBlockScript> ().Obstructed == 1) {
cell.GetComponent<CellScript>().Weight = 1000;
}
else{
cell.GetComponent<CellScript>().Weight = 500;
}
cell.GetComponent<CellScript>().Obstructed = block.GetComponent<ObstBlockScript>().Obstructed;
}
}
}
我没有得到错误,当我尝试使用调试工具时出现:这个请求不支持由被调试对象实现的协议版本。
根据要求,脚本的网格创建部分:void CreateGrid(){
Grid = new Transform[2,(int)GridSize.x,(int)GridSize.z];
for (int x=0; x<GridSize.x; x++) {
for (int z=0; z<GridSize.z; z++) {
Transform newBlock;
newBlock = (Transform)Instantiate (ObstBlockPrefab, new Vector3 (x/4f, 1.0f, z/4f), Quaternion.identity);
//newBlock.name = string.Format("({0},1,{1})",x,z);
newBlock.parent = transform;
newBlock.GetComponent<ObstBlockScript>().Position = new Vector3(x/4f,1.0f,z/4f);
Transform newCell;
Grid[1,x,z] = newBlock;
newCell = (Transform)Instantiate (CellPrefab, new Vector3 (x/4f, -0.125f, z/4f), Quaternion.identity);
//newCell.name = string.Format("({0},0,{1})",x,z);
newCell.parent = transform;
newCell.GetComponent<CellScript>().Position = new Vector3(x/4f,-0.125f,z/4f);
//if(GameObject.Find("ObstGrid").GetComponent<ObstructionScript> ().ObstGrid[x,z]./*GameObject.*/Find ("ObstBlock").GetComponent<ObstBlockScript>().obstructed == 1){
// newCell.GetComponent<CellScript>().Weight = 83;
//}
Grid[0,x,z] = newCell;
}
}
}
你的代码看起来很直接。所以,除非有什么奇怪的是发生在CellScript或ObstBlockScript重置值,我不得不猜测DetectObstructions()得到执行太早,在任何的ObstBlockScript之前。阻塞设置为"1"
这应该很容易调试使用断点或至少通过添加debug . log到DetectObstructions(),看看是否有任何的ObstBlockScript。