- Home /
Emptying a Generic List / Unexpected Behaviour
I am working on a simple tile based game and I have created a class container (TileLocations) to hold a number of variables that record useful information about a number of locations within each game tile. The one that is causing me problems is a generic list that I use to record a list of the current units that are present in that part of the game tile:
class TileLocations extends System.Object{
var unitsPresentList = new List.<GameObject >();
}
var tileLocation = new TileLocations[9];
This is probably not the most efficient way to do it, but it makes dealing with the logic of game behaviour much easier for me to understand (and it's all visible in the inspector when I click on each game tile). The problem is that I can't seem to get the array list to behave as I would expect. I am using the colliders to detect which units are present on the tile with:
var colliders : Collider[] = Physics.OverlapSphere (this.transform.position, searchRange);
for (var hit : Collider in colliders) {
if (!hit){
continue;
}
if (hit){
tileLocation[locationNumber].unitsPresentList.Add(hit.gameObject);
}
}
...and I am attempting to clear the generic list prior to filling it with the above code, by doing this:
function ClearArmiesInTileLocation(locationNumber: int){
tileLocation[locationNumber].armiesPresentArray.Clear();
tileLocation[locationNumber].armiesPresentArray.TrimExcess();
}
However, I seem to be ending up with some strange inconsistencies. Occasionally I will get "missing gameObject" in the array - I assumed this must have been when a unit is destroyed and is no longer references in the array, but I always clear the array prior to destroying units, so there should never be any units in the array at the time that the unit is destroyed. On occasion I also seem to be getting duplicates of the same objects in the arrays.
Am I misunderstanding how these things are handles in memory? Am I not actually removing everything from the arrays (hence things getting reported missing when I later destroy their gameObjects), or duplicates appearing?
Alternatively, are there any similar concepts that might work better? I'd rather not recode everything from the ground up, since this is actually efficient enough for my game (no noticeable slow down or hangs while rebuilding arrays) - I'd rather iron out my method, or refine the way in which objects are added and removed from arrays.
Thanks!
Try debugging the problem. For instance printing the Count
of your list after clearing it.
When you say: '"$$anonymous$$issing gameObject" in the array'. Do you mean the List
? Are you sure that you are not also rebuilding the list after clearing it?
It's very hard to give a definitive answer as you have not supplied all the necessary code. The code which you have supplied works splendidly (tested) :)
One thing, get rid of the TrimExcess line. You would normally only use it if you're not going to add any more elements to the list; using it like this will just cause unnecessary garbage collection.
Expanding on @Eric5h5's comment. It's also completely useless (and might actually slow it down in the future), unless you hugely gigantic list that you use very rarely and need the memory for something else :)
Benproductions1, thanks I will try debugging again in more detail, I may have given up too soon on that - I couldn't quite track the issue down last time, so had come the conclusion that it was likely something that I wasn't able to work out with my current (pretty limited) knowledge.
When I said missing gameObject in the array, I meant to say the unitsPresentList. In the inspector one of the entries in the list, which previously showed the name of the game object, would then show something about a missing object. From what I can gather, that means I have deleted the object before removing it from the list - but as far as I could tell I wasn't doing that.
Eric5h5, thanks I have removed all the TrimExcess() calls, I don't need those, based on what you said.
If I can't get it to work after another round of debugging I will try to get all the code out into here.
Thanks!
You have both:
tileLocation[locationNumber].unitsPresentList
tileLocation[locationNumber].armiesPresentArray
in your code, but we only see unitPresentList actually declared. Are you using two different lists concurrently to keep track of your units?
Answer by Android Matt · Jul 16, 2013 at 08:52 PM
I managed to locate the issue, with another focussed debug effort. However, I don't fully understand the reasoning for why the solution fixes my issue.
I was updating the unitsPresentArray first, then the armiesPresentArray (not fully mentioned above), but for some reason the update of the armiesPresentArray with Physics.OverlapSpehere was interfering with the first Physics.OverlapSphere. Printing a list of hit objects from the first showed that some hit in the second were also being counted as a hit in the first. I put "Yield;" between them and it seems to adequately separate the processes - however I don't understand why it needs that to work.
Regarding the missing objects, I had assigned the collider used in this process to variable, but I had forgotten to update my code to reflect that (I was accessing the wrong collider) so the object was never actually removed from the array, and consequently would return a missing object error when it was deleted.
Thanks!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
how to add list after removing some 1 Answer
How to find the opposite variables list? 1 Answer
how add values to Generic.list ? 2 Answers
Generic List.Count always gives 0 2 Answers