InvalidOperationException: Operation is not valid due to the current state of the object
While (queue.Peek() != null) throws this error. changing queue.Peek() to just queue skips that section of error and moves on to the next line of var q = queue.Dequeue();
I do know that the queue itself isn't empty, however curiously enough there are 3 null entries at the end, I'm not sure if this is normal for queues as they're grow-able or whatnot, but apparently any of those two Peek() and Dequeue() operations causes this. Enqueue() does not however as you can see it being done multiple times above the problem area.
The original code I'm basing myself off of instead uses queue.Any(), but this is apparently not available based on Unity's intellisense since it doesn't give it up as an option, even though it did somehow work out in the other project using Any(), maybe it was using a different compiler.
Appreciate any help.
private void AssignTileElevations ()
{
var queue = new Queue<Tile>();
foreach (var q in islandMap) {
q.water = !inside(q.point);
}
foreach (var q in islandMap)
{
// The edges of the map are elevation 0
if (q.border)
{
q.elevation = 0;
queue.Enqueue(q);
}
else
{
q.elevation = float.PositiveInfinity;
}
}
while (queue.Peek () != null) {
var q = queue.Dequeue();
foreach (var s in q.adjacent)
{
var newElevation = 0.01f + q.elevation;
if (!q.water && !s.water)
{
newElevation += 1;
if (needsMoreRandomness)
{
newElevation += UnityEngine.Random.value; ;
}
}
if (newElevation < s.elevation)
{
s.elevation = newElevation;
queue.Enqueue(s);
}
}
}
Debug.Log ("Elevations Set");
}
Answer by Luponius · Mar 29, 2016 at 05:51 PM
Apparently the problem was Peek(). For some reason you can't peek an array if it doesn't exist and even if it does exist it will still throw invalidoperation exceptions... I have no idea why this is but swapping to queue.Count > 0 instead of peek solved the issue.