The question is answered, right answer was accepted
Why is my list bigger than it should be?
I'm generating 3 random waypoints then saving them to a list but when debugged the list is showing a Count of 5, why?
function getWaypionts ()
{
for (var l=0; l < 3; l++)
{
Debug.Log("l is " + l);
var walkRadius : float = 10;
var randomDirection : Vector3 = Random.insideUnitSphere * walkRadius;
randomDirection += transform.position;
var hit : NavMeshHit;
NavMesh.SamplePosition(randomDirection, hit, walkRadius, 1);
var finalPosition : Vector3 = hit.position;
waypoints.Add(finalPosition);
}
for (var lk=0; lk < waypoints.Count; lk++)
{
Debug.Log("waypoint " + lk + " is " + waypoints[lk]);
}
waypointsGenerated = true;
}
Answer by Exceptione · Mar 04, 2016 at 01:33 PM
Do you have any in the inspector of the script in Unity that are already in the list before you run the game?
If not, make sure you're not adding them from anywhere else. At first glance I can't see anything wrong with the code.
O$$anonymous$$G, I'd left the var public and must have set it to 2 sometime, thats where they came from, cheers, convert your comment to an answer and I'll accept it.
Answer by phil_me_up · Mar 04, 2016 at 01:38 PM
As Exceptione said, make sure you don't have anything added to the list within the inspector.
I'd suggest making it a private variable, or perhaps tagging with [HideInInspector] if you don't actually want to be able to edit these values at edit time.
It might be sensible to run .Clear() on the list before you start adding your data too.
Follow this Question
Related Questions
nav mesh agent problem, can only be called on active agent that has been placed on a nav mesh 1 Answer
Spawning large numbers of navmeshagents causes them to jump position 0 Answers
Navmesh Agent trigger ? 1 Answer
Is there a way to modify my script a path, before setting it to a NavmeshAgent 0 Answers
=> How to Avoid NavMeshAgents Clumping Up Together?! <= 1 Answer