C# 将令牌移动限制到Unity3D中的相邻区域
在我的(TEG/RISK Wannab)2D游戏中,我在地图上有一个标记,我用对撞机将地图分成16个不同的区域,每个区域都有对撞机,这样我就可以点击它们并将标记移向它。我能做基本的动作:标记位置=区域位置,而且效果很好。当我尝试将移动限制到令牌相邻区域的算法时,问题来了,我得到了很多错误,当我没有得到错误时,它就不起作用了 这个脚本有200行长(是的,非常确定是-100%优化的),所以我要缩短一些部分。它应该连接到16个区域中的每一个C# 将令牌移动限制到Unity3D中的相邻区域,c#,unity3d,nullreferenceexception,C#,Unity3d,Nullreferenceexception,在我的(TEG/RISK Wannab)2D游戏中,我在地图上有一个标记,我用对撞机将地图分成16个不同的区域,每个区域都有对撞机,这样我就可以点击它们并将标记移向它。我能做基本的动作:标记位置=区域位置,而且效果很好。当我尝试将移动限制到令牌相邻区域的算法时,问题来了,我得到了很多错误,当我没有得到错误时,它就不起作用了 这个脚本有200行长(是的,非常确定是-100%优化的),所以我要缩短一些部分。它应该连接到16个区域中的每一个 using UnityEngine; using Syste
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Movimiento : MonoBehaviour {
//Prefabs of each zone
public Transform OBJ_ZONA_1;
public Transform OBJ_ZONA_2;
.
.
.
public Transform OBJ_ZONA_16;
//List of zones prefabs to optimize searchs
public List<Transform> LISTA_OBJ_ZONAS;
//List of each zone's neighbors
public List<int> LISTA_VECINOS_ZONA_1;
public List<int> LISTA_VECINOS_ZONA_2;
.
.
.
public List<int> LISTA_VECINOS_ZONA_16;
//List of lists of each zone's neighbors to optimize searchs
public List<List<int>> LISTA_LISTAS_VECINOS;
//The token object
public Transform FICHA;
public void MoverFicha(){
Vector3 TP = FICHA.transform.position;
TP.x = this.transform.position.x;
TP.y = this.transform.position.y;
FICHA.transform.position = TP;
}
//Function to clarify the neighbors of the token at the momment
public List<int> Vecinos(){
for (int i=0; i<15; i++) {
if (FICHA.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && FICHA.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
return LISTA_LISTAS_VECINOS[i];
}
return LISTA_VECINOS_ZONA_1;
}
//Function to define the zone that got triggered by the click
public int DefinirZonaClick(){
for (int i=0; i<15; i++) {
if (this.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && this.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
return i+1;
}
return 1;
}
void Awake(){
//Fill the list of zone objects
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_1);
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_2);
.
.
.
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_16);
//Clarify the neighbors of each zone
LISTA_VECINOS_ZONA_1.Add (2);
LISTA_VECINOS_ZONA_2.Add (1);
.
.
.
LISTA_VECINOS_ZONA_16.Add (15);
}
// Use this for initialization
void Start () {
//Fill the list of lists of neighbors of each zone
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_1);
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_2);
.
.
.
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_16);
}
// Update is called once per frame
void Update () {
//Define the list of neighbors for the token at the momment of trigger
List<int> VECINOS = Vecinos ();
//Define the clicked zone
int INT_ZONA_CLICK = DefinirZonaClick ();
//If the triggered zone is in the token's neighbors list then it moves towards that zone
if (VECINOS.Contains (INT_ZONA_CLICK))
MoverFicha ();
}
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共阶级运动:单一行为{
//每个区域的预制件
公共交通1区;
2区公共交通改造;
.
.
.
公共交通改造项目16区;
//用于优化搜索的区域预制列表
公共清单清单;
//每个区域的邻居列表
公共清单1;
公共清单2;
.
.
.
公共清单清单16;
//用于优化搜索的每个区域的邻居列表
公共列表列表;
//令牌对象
公共改革基金会;
公共无效MoverFicha(){
向量3 TP=FICHA.transform.position;
TP.x=this.transform.position.x;
TP.y=this.transform.position.y;
FICHA.transform.position=TP;
}
//函数用于澄清当前令牌的邻居
公共列表Vecinos(){
例如(int i=0;i您正在声明LISTA_VECINOS_ZONA_X
等,但您实际上没有给它们一个值/创建对象的实例(或者这是我可以从提供的代码中推断出来的)。因此,当您将它们添加到LISTA_LISTAS_VECINOS
时,您会得到一个空引用错误
创建LISTA\u VECINOS\u ZONA\u X
的实例,然后将它们添加到方法Start()
的列表中
编辑:
您的列表列表是在代码中声明的,但我看不到它实际上被实例化了。这是应该在声明的地方
public List<List<int>> LISTA_LISTAS_VECINOS = new List<List<int>>();
public List LISTA_LISTAS_VECINOS=new List();
LISTA_VECINOS_ZONA_X get在Awake()方法中声明的项目可能重复,该方法在Start()之前触发,即使在Start()方法中添加项目,我也会遇到相同的错误。您能显示Moveitem.cs第154行的代码吗?