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;
}
}