- Home /
Nodes are deleted from the list for no reason
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
After the start of the game
is there really no other script interacting with this? do you exit play mode to make the list changes? changes are only saved when you are not in play mode.
There is another script that calls to it and that this script is connected then the problem happens I forgot to attach it
According to the debugging I did, I saw that the code did not enter the for in line 60 according to the code I sent below and the code jumps to line 76 and returns a blank list and then the node disconnects from the list
Answer by yair710 · Jun 22, 2021 at 07:27 AM
public class botAiScript : MonoBehaviour
{
public List<NodeScript> AllNodes = new List<NodeScript>();
public NodeScript ClosestNode;
public NodeScript TargetNode;
public Transform Target;
public List<NodeScript> Path;
public Movement mvmt;
public float minDist;
public float maxDist;
public List<NodeScript> result;
void Awake()
{
//Path = new List<NodeScript>();
result = new List<NodeScript>();
AllNodes = FindObjectsOfType<NodeScript>().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<NodeScript> Breadthwise(NodeScript start, NodeScript end)
{
Debug.Log("2");
result = new List<NodeScript>();
List<NodeScript> visited = new List<NodeScript>();
Queue<NodeScript> work = new Queue<NodeScript>();
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[i];
if (!visited.Contains(currentNeighbor))
{
Debug.Log("7");
currentNeighbor.history = new List<NodeScript>(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();
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
moving object toward waypoint and alignment 0 Answers
Confused about copying Lists! 1 Answer
Make object move through waypoints by the given number in the name 1 Answer
How can i detect how many objects of the same type i'm triggering and choose only one of them? 1 Answer