Ive been banging my head against the wall for the last 2 hours trying to figure this out…
I have an object that has a public List in it. That list has 1 entry in it, put into it by me in the editor mode.
I start the game up, and check the count. Any objects that only have 1 object in their lists return 0 for the count, while anything more than 1 returns the correct value.
At no time are these lists being recreated, added to, removed from. I call the count while looking directly at the 1 entry in the list and still nothing.
public void FindRoute(Waypoint ending)
{
List<Waypoint> uncheckedWaypoints = new List<Waypoint>();
for(int x = 0; x < waypointCollection.transform.GetChildCount(); x++)
{
uncheckedWaypoints.Add (waypointCollection.transform.GetChild(x).GetComponent<Waypoint>());
}
finishedDestination = ending;
finishedDestination.myDistance = 0;
while(uncheckedWaypoints.Count > 0)
{
Waypoint shortest = null;
foreach(Waypoint way in uncheckedWaypoints)
{
if(shortest == null)
shortest = way;
if(way.myDistance < shortest.myDistance)
shortest = way;
}
print (shortest.connections.Count);
uncheckedWaypoints.Remove(shortest);
if(shortest.myDistance == Mathf.Infinity)
break;
foreach(Waypoint connection in shortest.connections)
{
float newDistance = shortest.myDistance + (Vector2.Distance(shortest.gameObject.transform.position, connection.gameObject.transform.position));
if(newDistance < connection.myDistance)
{
connection.myDistance = newDistance;
connection.previousWaypoint = shortest;
}
}
}
}
Calling this function on a waypoint that has only 1 connection in its list returns a 0 count.
The Waypoint class has nothing in apart from the distance and connections variables.
Edit. Attached relevant files.
See the following link for the two cs files.
http://www.plusintstudios.com/Files