0

I'm getting an error while developing FPS game in Unity 3D

NullReferenceException: Object reference not set to an instance of an object Node.OnDrawGizmos () (at Assets/Node.cs:14)

It working fine earlier but when I added layer to the nodes then I got this error.

check out with the complete code

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

public class Node : MonoBehaviour {

public List<GameObject> neighours = new List<GameObject>();

public float nodeRadius = 50.0f;
public LayerMask nodeLayerMask;
public LayerMask collisionLayerMask;

public GameObject goal;

void OnDrawGizmos() {

    Gizmos.DrawWireCube(transform.position, Vector3.one);

    foreach(GameObject neighbor in neighbors) {

        Gizmos.DrawLine(transform.position, neighbor.transform.position);
        Gizmos.DrawWireSphere(neighbor.transform.position, 0.25f);
    }

    if(goal) {

        GameObject current = gameObject;
        Stack<GameObject> path = DijkstraAlgorithm.Dijkstra(GameObject.FindGameObjectsWithTag("Node"), gameObject, goal);

        foreach(GameObject obj in path) {

            Gizmos.DrawSphere(obj.transform.position, 1.0f);
            Gizmos.color = Color.green;
            Gizmos.DrawLine(current.transform.position, obj.transform.position);
            current = obj;
        }
    }
}

[ContextMenu ("Connect Node to Neighours")]
void findNeighours() {

    neighours.Clear();
    Collider[] cols = Physics.OverlapSphere(transform.position, nodeRadius, nodeLayerMask);

    foreach(Collider node in cols) {

        if(node.gameObject != gameObject) {


        }
    }
}

}

Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
Ravi Pal
  • 385
  • 1
  • 7
  • 22
  • Check for null or empty values – Sajeetharan Mar 29 '13 at 06:54
  • 1
    which line does cause the null reference exception? try to step and understand where the exception is raised, otherwise is impossible to give you an answer. – Heisenbug Mar 29 '13 at 08:36
  • is `goal` set to anything? It could possibly be what's throwing the error. – Steven Mills Mar 29 '13 at 10:09
  • @StevenMills `goal` [doesn't need to be instantiated](http://docs.unity3d.com/Documentation/ScriptReference/Object-operator_bool.html?from=GameObject) for `if(goal)` to work. – Jerdak Mar 29 '13 at 12:49
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – LearnCocos2D Feb 16 '15 at 10:45

1 Answers1

0

I see findNeighours does not feed neighours, so I imagine you want something like:

foreach (Collider node in cols) {
    if (node.gameObject != gameObject)
        neighours.Add (node.gameObject);
}

About OnDrawGizmos, potentially path and neighours could be holding null items. You should check if that's the case and see why it's filling with it nulls. Note that probably removing a GameObject in the scene (and not refreshing using findNeighours) could make it hold null references.

Check if you have defined goal.

Note: by neighours you mean neighbors?

frarees
  • 2,034
  • 3
  • 17
  • 22