0

Within this line (below), I get a NullReferenceException.

float distance = Vector3.Distance(enemyTankObject.transform.position, graph.getVertexList()[index].getVertex().position); 

So, I started using Debug.Log() in order to figure out what exactly is causing the problem. The problem is the graph variable.

Now, I am not exactly sure why it is happening - the only other line of code that I have on the graph variable is the following:

private Graph graph;

This line of code is within a class, not within a method of the class, basically, graph is an attribute of the class of type Graph.

I did research this problem and tried many ways to solve but it just did not work, that's why I am posting this question here.


EDIT:

The Graph class was made by me. But since it's quite a bit of code and none of it creates this bug I thought not to add it. If you want to see it though, this is it:

public class Graph : MonoBehaviour
{
    [SerializeField] Transform[] vertexListSetup; //this is set up by me in the Unity Editor
    private Vertex[] vertexList; //this is set up by me in the Start() method
    private int graphIndex = 0;
    void Start()
    {
        vertexList = new Vertex[6];

        //adding vertexes
        int vertexNo = 0;
        while (vertexNo < 6)
        {
            addVertex(vertexListSetup[vertexNo]);
            Debug.Log("Type of item at vertexNo " + vertexNo + ": " + vertexList[vertexNo]);
            Debug.Log("Data within the item at vertexNo " + vertexNo + ": " + vertexList[vertexNo].getVertex());
            if (vertexNo > 0)
            {
                Debug.Log("Type of item at vertexNo-1 " + (vertexNo-1) + ": " + vertexList[vertexNo-1]);
                Debug.Log("Data within the item at vertexNo-1 " + (vertexNo-1) + ": " + vertexList[vertexNo-1].getVertex());
            }

            vertexNo++;
        }

        //adding edges
        int edgeNo = 0;
        while (edgeNo < 5)
        {
            addEdge(vertexListSetup[edgeNo], vertexListSetup[edgeNo + 1], Vector3.Distance(vertexListSetup[edgeNo].position, vertexListSetup[edgeNo + 1].position));
            edgeNo++;
        }


    }

    // Update is called once per frame
    void Update()
    {

    }

    public void addVertex(Transform data)
    {
        Debug.Log("data: " + data);

        var vertex = new Vertex(data);
        Debug.Log("vertex: " + vertex);

        vertexList[graphIndex] = vertex;

       graphIndex++;
    }

    public Vertex findVertex(Transform needle)
    {
        int v = 0;
        bool found = false;
        while (v < vertexList.Length && found != true)
        {
            if (vertexList[v].getVertex() == needle)
            {
                found = true;
            }
            else
            {
                v++;
            }
        }

        Debug.Log("Found: " + found);
        if (found)
        {
            return vertexList[v];
        }
        else
        {
            return null;
        }
    }

    public void addEdge(Transform a, Transform b, float weight)
    {
        Debug.Log("Transform a: " + a);
        Debug.Log("Transform b: " + b);

        Vertex v1 = findVertex(a);
        Vertex v2 = findVertex(b);

        Debug.Log("v1: " + v1);
        Debug.Log("findVertex(a): " + findVertex(a));
        if (v1 == null || v2 == null)
        {
            Debug.Log("Error - vertex not found");
        }


        Debug.Log("Edge: " + a + " = " + v1 + " and " + b + " = " + v2);

        var e1 = new Edge(v1, v2);
        var e2 = new Edge(v2, v1);

        e1.setWeight(weight);
        e2.setWeight(weight);
        v1.addEdge(e1);
        v2.addEdge(e2);
    }

    public Vertex[] getVertexList()
    {
        return vertexList;
    }

    public Transform[] getTransformList()
    {
        return vertexListSetup;
    }


}
eight8
  • 3
  • 2
  • 2
    so when is `graph` initialized with a value? Also `Graph` is no Unity built-in type so please add it to your question – derHugo Mar 23 '20 at 09:45
  • Okay, I added it to my question. Now, what do you mean when it is initialized with a value? Like ```GetComponent```? In that case, never... because I don't see why I should. The ```graph``` class is attached to an empty game object made of 6 empty game objects which act as ```vertexes``` for the ```graph```. – eight8 Mar 23 '20 at 10:31
  • well yes but if you never tell your class which reference shall be stored in the field `graph` then it will always have its default value `null` ... this is basic c# ... Unity does no further magic here. You should either use `GetComponent` or make the field `[SerializeField]` and reference the component via the Inspector – derHugo Mar 23 '20 at 10:35
  • Oh, damn, thank you very much, that solved it. I just added ```[SerializeField]```, attached the component and it worked. Thanks again, have an amazing rest of the day. – eight8 Mar 23 '20 at 11:15

0 Answers0