- Home /
List.Count returning 0 while it has 1 entry
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
The very first thing you do at the top of this function is reset your list. Is this intentional?
Where do you call the function? Also is it possible that you recreate the "connections" list somewhere? If it's a public variable it get initialized automatically when the object is deserialized, so you shouldn't create it anywhere except the class constructor or as field initializer. But you actually don't need to create it.
I use Lists all the time, so i'm pretty sure that count works as expected ;)
apart from everything else, don't you need to mark it nonserialised, so the editor won't screw with it?
@SirGive That list is a different list that gets poppulated with all the waypoints.
@Bunny83 This function is called when the user clicks the mouse. It takes the mouse coords and figures out the closest waypoint, that is sent to this function. As for recreating the connections list, that doesnt happen. The only place it gets created is in the editor before being run.
@Fattie The editor is where all of the connections are set manually by myself.
"The editor is where all of the connections are set manually by myself."
make it nonserialised.
and/or use the amazing little-known "RESET" button which is attached to the inspector on the small dropdown there. check it out.
note that values you set in the editor "fight with" defaults you set in the script
Answer by Bunny83 · May 05, 2013 at 10:40 AM
I suggest you put in this script above a public variable like this:
public Waypoint debugWP;
And where you print your connections.Count do this:
debugWP = shortest;
Now you can click on the variable in the inspector and Unity will "ping" the object your function has choosen.
I guess the function uses a different Waypoint than the one you expect.
I have just create a similar Waypoint script. I created 5 Waypoint objects and connect them in a line, so the first and last only has one connection the 3 in the middle have 2 connections. I added this function to the Waypoint script:
void Start()
{
Debug.Log("WP: " + name + " : " + connections.Count);
}
And it prints 2 for the middle 3 WPs and 1 for the first and last one. There has to be something else...
Oh and btw, you should print a meaningful log message. Just printing a number doesn't tell you who printed it and what it actually means. I've seen a lot people doing what you did and their problem was that the log message they got was from another script and not the one they expect. Are you sure that your function is called? ;)
You know that this line:
Vector3 clickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Will give you always just the position of the camera. That's simply because ScreenToWorldPoint expects a 2D cordinate in the x and y part of the given vector and the z part defines the distance from the camera. Since you directly pass Input.mousePosition the z value will be 0.
If you want a position in the world you should use a raycast to deter$$anonymous$$e the position. It actually doesn't make any difference where you click, it always gives you the camera position. Also the "On$$anonymous$$ouseDown" even seems a bit strange here. I guess the Click$$anonymous$$ap script is on your terrain / map / world object? In this case it will work but since you need to raycast anyways i would handle all this in Update if the mousebutton is pressed.
"It is the only place that prints in my program"
It is not possible to think that way as a programmer.
You THIN$$anonymous$$ that's the only place it prints in your program. Do just what bunny says, add descriptive text inside the Log statement ok!
To quote the bad guy from Under Siege 2: "Assumption is the mother of all f$#!ups.
Answer by squigglyo · May 06, 2013 at 12:42 PM
An update on the situation.
I have a fix... but it defies logic. The count will return the correct value, after I switch from Game, to the Scene tab and back to the Game tab out.
This doesnt work in a build however as actually tabbing to another window doesnt seem to do anything.