Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Android Matt · Jul 13, 2013 at 11:10 PM · arraylistmemorygeneric list

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!

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Jul 14, 2013 at 03:46 AM 1
Share

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) :)

avatar image Eric5h5 · Jul 14, 2013 at 03:57 AM 1
Share

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.

avatar image Benproductions1 · Jul 14, 2013 at 04:00 AM 1
Share

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 :)

avatar image Android Matt · Jul 14, 2013 at 05:48 AM 0
Share

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!

avatar image Jamora · Jul 14, 2013 at 10:30 AM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges