unity error null引用异常:gameobject的实例未设置为gameobject的引用

时间:2017-12-05 13:33:10

标签: c# unity3d nullreferenceexception

我收到错误空引用异常:对象引用未设置为对象的实例。我相信这是因为我的玩家pos和anemy pos是null但我不明白为什么他们会为null。抱歉重新发布,因为我已经看到了其他地方,但无法设法解决

public class Node : MonoBehaviour

{
public GameObject[] neibors;
public Vector2[] directions;

public static int[] distances = new int[4];
static float distance;

int i = 0;

public static GameObject PlayerPos = null, EnemyPos = null;


// Use this for initialization

void Start()
{

    var box = gameObject.AddComponent<BoxCollider2D>();
    box.isTrigger = enabled;

    foreach (GameObject item in neibors)
    {

        //find distance.
        distance = (gameObject.transform.localPosition.x - item.transform.localPosition.x) + (gameObject.transform.localPosition.y - item.transform.localPosition.y);
        distance = Mathf.Abs(distance);
        int distanc = (int)distance;
        distances[i] = distanc;
        i++;


    }


}

public void OnTriggerEnter2D(Collider2D other)
{
    if (gameObject.tag == "pallet" && other.name == "pacman_1")
    {

        EnemyPos = gameObject;

    }
    if (gameObject.tag == "pallet" && other.name == "player")
    {


        PlayerPos = gameObject;

    }

}
public void Main()
{

    Graph g = new Graph();
    if (neibors.Length == 2)
    {
        g.add_vertex(gameObject, new Dictionary<GameObject, int>() { { neibors[0], distances[0] }, { neibors[1], distances[1] } });
        Debug.Log(gameObject);
    }
    if (neibors.Length == 3)
    {
        g.add_vertex(gameObject, new Dictionary<GameObject, int>() { { neibors[0], distances[0] }, { neibors[1], distances[1] }, { neibors[2], distances[2] } });
        Debug.Log(gameObject);
    }
    if (neibors.Length == 4)
    {
        g.add_vertex(gameObject, new Dictionary<GameObject, int>() { { neibors[0], distances[0] }, { neibors[1], distances[1] }, { neibors[2], distances[2] }, { neibors[3], distances[3] } });
        Debug.Log(gameObject);
    }

    g.shortest_path(PlayerPos, EnemyPos).ForEach(x => Debug.Log(x));

}

}         公共类图         {         字典&GT; vertices = new      字典&GT;();

    public void add_vertex(GameObject gameObject, Dictionary<GameObject, int> edges)
    {
        vertices[gameObject] = edges;

    }
    public List<GameObject> shortest_path(GameObject PlayerPos, GameObject EnemyPos)
    {
        var previous = new Dictionary<GameObject, GameObject>();
        var dist = new Dictionary<GameObject, int>();
        var nodes = new List<GameObject>();

        List<GameObject> path = null;

        foreach (var vertex in vertices)
        {
            if (vertex.Key == PlayerPos)
            {
                dist[vertex.Key] = 0;

            }
            else
            {
                dist[vertex.Key] = int.MaxValue;
            }

            nodes.Add(vertex.Key);
        }

        while (nodes.Count != 0)
        {
            nodes.Sort((x, y) => dist[x] - dist[y]);

            var smallest = nodes[0];
            nodes.Remove(smallest);

            if (smallest == EnemyPos)
            {
                path = new List<GameObject>();
                while (previous.ContainsKey(smallest))
                {
                    path.Add(smallest);
                    smallest = previous[smallest];
                }

                break;
            }

            if (dist[smallest] == int.MaxValue)
            {
                break;
            }

            foreach (var neighbor in vertices[smallest])
            {
                var alt = dist[smallest] + neighbor.Value;
                if (alt < dist[neighbor.Key])
                {
                    dist[neighbor.Key] = alt;
                    previous[neighbor.Key] = smallest;
                }
            }
        }

        return path;
    }

}

0 个答案:

没有答案