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 camtronius · Jun 11, 2014 at 03:45 PM · objectlistdestroyarraysrts

How to destroy a unit contained in multiple lists

Hey guys, I have a few lists in my 3D RTS game:

  • EnemyArray

  • EnemyList

  • DistanceToEnemyList

  • SortedDistanceToEnemyList.

I was wondering if this is the best approach? My problem is that when multiple players are fighting the same enemy, and one of the players kills the enemy (using destroy object), it pings "array out of bounds exception" or that "the object is still trying to be referenced". I have a condition where it clears all the lists after the enemy is dead , but I think the other player unit still thinks its alive at that point. Ive tried putting a bunch of if(blah!=null) as well. Does anyone have another approach or method they have used to solve this problem? Thanks,

EnemyArray is run with an IEnumerator every 0.5 seconds and it detects every enemy object that is spawned in the level. It then adds the object from that array to EnemyList at that index as shown in the code below,

    for(int i=0 ; i<EnemyArray.Length; i++){ //iterates through the array and adds all objects to the list
      if(EnemyArray[i]!=null){
      EnemyList.Insert(i, EnemyArray[i]); //adding to the list
      }
    }

Next Distance is calculated between the player unit and the enemy unit using a for loop to iterate through the EnemyList. The distance values are stored in DistanceToEnemyList. This list is then sorted as SortedDistanceToEnemyList. Here i take the minimum index [0] as the minimum distance.

 for(int i=0; i< EnemyList.Count; i++){
  
          if(EnemyList[i] !=null){
  
          float distance = Vector3.Distance (transform.position, EnemyList[i].transform.position); //finds distance between enemy and player
  
          DistanceToEnemyList.Insert(i,distance); //inserts the distance values at the indicies
  
          SortedDistanceToEnemyList.Insert(i,distance); //this is the same as distancelist but will be sorted
  
          SortedDistanceToEnemyList.Sort();//sorts the list in ascending order
  
          //min distance is the first index of sorted list...
          }
        }

I then define a variable called minDistanceIndex. Which is the zero index of SortedDistanceList. This will give me the minimum distance value:

      minDistanceIndex = DistanceToEnemyList.IndexOf(SortedDistanceToEnemyList[0]);
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Chris_Dlala · Jun 11, 2014 at 09:28 PM

Hi, having so many synced arrays scares me a little - it'll be very hard to ensure they all have the same values at all times. To find the minimum distance I would do a quick search of the entire Array/List:

 int closestIndex = 0;
 float closestDistance = float.Max;
 
 for(int i = 0; i < EnemyList.Count; i++)
 {
     // Test for dead enemies
     if(!EnemyList[i])
     {
         // Move on to next
         continue;
     }
     else
     {
         d = Vector3.Distance (transform.position, EnemyList[i].transform.position);
         if(d < closestDistance)
         {
             closestDistance = d;
             closestIndex = i;
         }
     }
 }

Sorting a list does more comparisons than simply finding the current best/closest. This should also gracefully move on and exclude destroyed enemies from the search. Only one list (or array if you prefer) for any number of players accessing it that you can update as often as you like. I hope that helps =)

Comment
Add comment · Show 1 · 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
avatar image camtronius · Jun 12, 2014 at 05:28 AM 0
Share

@Chris_Dlala Hey Chris, THanks for the awesome code. Everything worked, except when the player unit is detecting an enemy it always thinks the enemy with the zero index is the closest enemy and I do not know why.. Do you know why it would do this? Do you think having my list as "public static" in my other class would do this? Thanks,

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

21 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 avatar image avatar image avatar image

Related Questions

destroying a unit and removing it from a list 2 Answers

A node in a childnode? 1 Answer

Having Multiple Controllable FPS Units Selected From A Singular RTS Mode? 0 Answers

Destroying Gate by hitting a switch 1 Answer

Problem with destroying a gameObject when used with targeting 1 Answer


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