A* problem

Hello I built a script thats supposed to be the AI pathfinding system, but it fails to build the route after all the connections were established here is the code:

private List<Connection> route = new List<Connection>();// the route to head in
	
	//calculates the shortest way from point a to point b
	public List<Connection> SetRoute(Connection[] graph, Node start, Node end, float heuristic)
	{
		List<NodeRecord> nrs = new List<NodeRecord>();
		//Initialize data
		NodeRecord startRecord = new NodeRecord();//create a record for the start node
		startRecord.costSoFar = 0f;
		startRecord.n = start;
		startRecord.c = null;
		startRecord.EstimatedCost = Mathf.Abs(Vector3.Distance(start.Location, end.Location));
		startRecord.EstimatedCost -= heuristic;
		
		List<NodeRecord> open = new List<NodeRecord>();// holds opened noderecords
		open.Add(startRecord);
		List<NodeRecord> closed = new List<NodeRecord>();// holds closed noderecords
		
		NodeRecord current = new NodeRecord();// create a noderecord for our current node
		
		for(int i = 0; i < open.Count; i++)// loop through the opened nodes
		{
			current = open[0];
			float est = open[0].EstimatedCost;// get estimated cost
			//Finding the smallest element in the list
			for(int j = 0; j < open.Count; j++)//get smallest element in the opened nodes
			{
				if(open[j].EstimatedCost < est)// if its smaller than our current minimal distance
				{
					est = open[j].EstimatedCost;
					current = open[j];
		//			Debug.Log("current estimated shit " + current.EstimatedCost.ToString());// seems to work
				}
			}
			if(current.n == end)// if its the last node terminate
			{
				break;// we finished finding the path
			}
			else
			{// we still search for a path
				Connection[] cons = graph;// gets the current noderecord outgoing connections
				foreach(Connection con in cons)//loops through all the connections
				{	
					Node endNode = con.To;// gets the end node
					float endNodeCost = current.costSoFar + con.Distance;// update and store the distance
					
					Debug.Log("End node cost " + endNodeCost + ", Connecting between "  + con.From.n.Location.ToString() + " amd " + con.To.Location.ToString());// works
					
					//if closed list contains this end node
					float enHeuristic = 0f;
					NodeRecord endNodeRecord = Contains(closed, endNode);// check if this node is closed
					if(endNodeRecord.n != null)//if it is
					{
						if(endNodeRecord.costSoFar <= endNodeCost)// if the cost so far is smaller/equal to this cost
							continue;// continue to next node (breaks this if block)
						else// if it is not smaller
						{
							closed.Remove(endNodeRecord);// remove this node from the closed list
						}
						enHeuristic = endNodeRecord.EstimatedCost = endNodeRecord.costSoFar;// get the heuristic
					}
					else// if it is not closed
					{
						endNodeRecord = Contains(open, endNode);// check if this node is opened
						if(endNodeRecord.n != null)// if it is opened
						{
							if(endNodeRecord.costSoFar <= endNodeCost)// if the cost so far is smaller/equal to this cost
							{
								continue;// continue to next node (breaks this if block)
							}
							enHeuristic = endNodeRecord.EstimatedCost - endNodeRecord.costSoFar;// get the heuristic
						}
						else// if its not opened and closed (unvisited)
						{
							endNodeRecord = new NodeRecord();// create new record
							endNodeRecord.n = endNode;// set the node
							enHeuristic = Mathf.Abs(Vector3.Distance(endNode.Location, end.Location));//estimate the heuristic
						}
						endNodeRecord.c = con;// set the connection
						endNodeRecord.costSoFar = endNodeCost;//set the endcost
						endNodeRecord.EstimatedCost = enHeuristic + endNodeCost;//set the estimatedcost
						
						nrs.Add(endNodeRecord);
							
						if(!ContainsItem(open, endNodeRecord.n))
						{
							open.Add(endNodeRecord);// this node is opened
						}
					}
						
					open.Remove(current);// this node is no longer opened
					closed.Add(SetConnection(graph, current));// it is now closed
				}
			}
		}
		if(current.n != end)// if we couldn't get to the end node
		{
			Debug.Log("Error could not find a valid route");// meaning we don't have a route
			return null;// return nothing
		}
		else// else
		{
			while(current.n != start)// add the noderecords we found to the path
			{
		//		Debug.Log("Path " + current.c.From.c.From.n.Location.ToString());
				route.Add(current.c);// add the current connection
				current = current.c.From;
			}
			Debug.Log("Path is ready");// we successfully created a path
			SortNodes();
			return null;
		}
		return null;
	}

private NodeRecord SetConnection(Connection[] graph, NodeRecord current)
{
	for(int i = 0; i < graph.Length; i++)
	{
		if(graph*.From.n == current.n)*
  •   	{*
    

_ current.c = graph*;_
_
}_
_
}_
_
return current;_
_
}_
_
private void SortNodes()_
_
{_
_
List nodes = new List();_
_
for(int i = 0; i < route.Count; i++)_
_
{_
_
nodes.Add(route[route.Count - 1 - i]);_
_
}_
_
route = nodes;_
_
}_
_
private NodeRecord Contains(List list, Node endNode)_
_
{_
_
NodeRecord enr = new NodeRecord();_
_
for(int i = 0; i < list.Count; i++)_
_
{_
_ if(list.n == endNode)
return list;
}
return enr;
}
private bool ContainsItem(List list, Node endNode)
{
NodeRecord enr = new NodeRecord();
for(int i = 0; i < list.Count; i++)
{
if(list.n == endNode)
return true;
}
return false;
}
the problem occurs here:
while(current.n != start)
{
route.Add(current.c);
current = current.c.From;// note From is type of NodeRecord not node
}
classes:
public class NodeRecord
{
public float costSoFar;// the distance so far
public float EstimatedCost;// the estimated cost*

* public Connection c;// the connection*
* public Node n;// the node*
}
public class Node {_

* private Vector3 location;// the location of the node*
* private List connections;// the connected nodes*

* public Node()*
* {*
* location = new Vector3(0, 0, 0);*
* connections = new List();*
* }*
* public Vector3 Location*
* {*
* get{return location;}*
* set{location = value;}*
* }*
* public List Connections*
* {*
* get{return connections;}*
* set{connections = value;}*
* }*
}
public class Connection {

* private float distance;// the distance between the two nodes*
* private NodeRecord fromNode;// the node it came from*
* private Node toNode;// the node we travel to*

* public Connection()*
* {*
* distance = 0;*
* fromNode = new NodeRecord();*
* toNode = new Node();*
* }*
* public float Distance*
* {*
* get{return distance;}*
* set{distance = value;}*
* }*
* public NodeRecord From{*
* get{return fromNode;}*
* set{fromNode = value;}*
* }*
* public Node To{*
* get{return toNode;}*
* set{toNode = value;}*
* }*
* public Node GetToNode()*
* {*
* return null;*
* }*
}

Well after some time I found the answer though it’s very bad solution but it’s what I’ve got here it is:

while(current.c.From != start)
			{
				route.Add(current.c);
				for(int i = 0; i < nrs.Count; i++)
				{
					if(nrs*.n() == current.c.From())*
  •  			{*
    

_ current = nrs*;_
_
}_
_
}_
_
}_
_
Debug.Log(“Path is ready”);// we successfully created a path*_
* float distance = Mathf.Abs(Vector3.Distance(start.Location, route[0].From.Location));*
* Connection c = new Connection();*
* SortNodes();*
* c.Distance = distance;*
* c.From = start;*
* c.To = route[0].From;*
* route.Insert(0, c);*
note nrs is a list that stores every endnoderecord and after finishing I use it to restore all records since the current.c.From.n keeps giving me errors
if anyone got a better solution please notify me