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 EightbitPoe · Mar 25, 2015 at 11:37 PM · triggerlisthash

Removing element from a list after it's destroyed

I'm trying to build a TD game, and in order to handle multiple enemies at once, I'm using a hashtable and a list. The logic's been working really fine, but I'm having trouble removing the enemy after it's killed.

The problem is I'm using messages to manage enemy add/removal from the list, using trigger colliders. When the enemy enters tower range, it adds to both the list and the hash and starts shooting. When it leaves, the tower removes the enemy from both the list and the map and switches target. When the enemy is destroyed, however, it doesn't trigger my "OnTriggerExit2D" function and, thus, is not removed from the list nor the map.

Comment
Add comment · Show 2
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 maccabbe · Mar 25, 2015 at 11:52 PM 0
Share

Have the enemy keep a list of lists it's in.

avatar image bartm4n · Mar 26, 2015 at 01:07 AM 0
Share

How are you destroying the enemy? By calling the native Destroy() method?

2 Replies

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

Answer by Hrungdak · Mar 26, 2015 at 07:42 AM

I would do the following:

  • Put the code to remove an enemy from list and hashtable in an extra method of the tower script, e.g. "RemoveEnemyFromList(GameObject enemy)".

  • call this method from OnTriggerExit2D

  • in the tower update function, check if all enemies in the list are still alive.

  • if an enemy has died, call RemoveEnemyFromList(enemy) from the Update-Function.

Comment
Add comment · Show 3 · 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 EightbitPoe · Mar 26, 2015 at 03:23 PM 0
Share

That's a nice idea, thank you, sir! $$anonymous$$y only question is: how bad will it affect the performance?

avatar image Hrungdak · Mar 26, 2015 at 04:34 PM 0
Share

Performance hit should be manageable.

First its a tower defense. How much enemies can one tower have at the same time? 10? 20? No Problem. Second, its a question of architecture. If your enemy-gameobject has a method public bool IsDead(), then its a method call, not more.

For a greater amount of enemies there might be better solutions. But up to 100 enemies per tower i would do it as described.

avatar image EightbitPoe · Mar 27, 2015 at 12:18 AM 0
Share

I've tried it and, for some reason, this condition always return false: if (enemies[key] == null) (enemies is a GameObject hashtable)

Even if the enemy has already been destroyed, any idea why?

avatar image
0

Answer by kkxzd · Mar 26, 2015 at 12:43 AM

Really depends on how you have everything setup, but assuming you can make your list and map static members of your tower script:

 public static List<int> test_list = new List<int>() {1, 2};

You can then update the value from any other script just referencing the tower script, so you could run that on the OnDestroy method:

 OnDestroy()
 {
     TowerScript.test_list.Add(3);
 }

Just modifying the list here as you didn't provide any specific code, but it should be easy to modify to fit your needs.

Comment
Add comment · Show 4 · 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 kkxzd · Mar 26, 2015 at 02:34 AM 0
Share

$$anonymous$$aybe even better it would be to have the state in an independent class, not inheriting from $$anonymous$$onoBehaviour, and you can update the property from both the Tower and the enemies. You'll have to add additional info if there is more than one Tower thou.

avatar image Hrungdak · Mar 26, 2015 at 07:36 AM 0
Share

I dont think that this works. The static list would be the same for ALL towers in the game. As i understand this, you need an extra list for every tower.

avatar image kkxzd · Mar 26, 2015 at 02:20 PM 0
Share

He didn't clarify if it's one tower or multiple ones. Agree that it won't work in that case.

avatar image EightbitPoe · Mar 26, 2015 at 03:23 PM 0
Share

Sorry for not specifying that, and it's meant for multiple towers. But, using your idea, I can create a list in the enemy with the towers that are targeting it, and, upon destruction, it sends a message to all of them to remove it from their list.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Creating a list with trigger 1 Answer

Same Hash For Two Different List ? Why ? How to solve 1 Answer

i need to copy a list from the collided to the newly instantiated one individually . 0 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