I have a list of nodes that I built manually and every time I click start a game there is a node in the list that is deleted and I can not understand why
the code
public class NodeScript : MonoBehaviour
{
public NodeScript[] neighbors;
public List<NodeScript> history = new List<NodeScript>();
public void OnDrawGizmos()
{
Gizmos.DrawIcon(transform.position, "blendsampler");
foreach(var node in history)
{
Gizmos.DrawLine(transform.position, node.transform.position);
}
}
}
before
[182429-לפני.png|182429]
After the start of the game
[182430-אחרי.png|182430]
public class botAiScript : MonoBehaviour
{
public List AllNodes = new List();
public NodeScript ClosestNode;
public NodeScript TargetNode;
public Transform Target;
public List Path;
public Movement mvmt;
public float minDist;
public float maxDist;
public List result;
void Awake()
{
//Path = new List();
result = new List();
AllNodes = FindObjectsOfType().ToList();
}
NodeScript GetClosestNodeTo(Transform t)
{
NodeScript fNode = null;
float minDistance = Mathf.Infinity;
foreach (var node in AllNodes)
{
float distance = (node.transform.position - t.position).sqrMagnitude;
if (distance < minDistance)
{
minDistance = distance;
fNode = node;
}
}
return fNode;
}
private List Breadthwise(NodeScript start, NodeScript end)
{
Debug.Log(“2”);
result = new List();
List visited = new List();
Queue work = new Queue();
start.history = new List<NodeScript>();
visited.Add(start);
work.Enqueue(start);
while (work.Count > 0)
{
Debug.Log("3");
NodeScript current = work.Dequeue();
if (current == end)
{
Debug.Log("4");
//Found Node
result = current.history;
result.Add(current);
return result;
}
else
{
//Didn't find Node
Debug.Log("5");
for (int i = 0; i < current.neighbors.Length; i++)
{
Debug.Log("6");
NodeScript currentNeighbor = current.neighbors*;*
if (!visited.Contains(currentNeighbor))
{
Debug.Log(“7”);
currentNeighbor.history = new List(current.history);
currentNeighbor.history.Add(current);
visited.Add(currentNeighbor);
work.Enqueue(currentNeighbor);
}
}
}
}
//Route not found, loop ends
return null;
}
void Update()
{
if (!GetClosestNodeTo(Target).Equals(TargetNode))
{
Debug.Log(“1”);
TargetNode = GetClosestNodeTo(Target);
ClosestNode = GetClosestNodeTo(transform);
Breadthwise(ClosestNode, TargetNode);
}
//MoveTowardsPath();
}
}