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 Stealthygolem · Dec 14, 2014 at 08:59 PM · c#arraytriggerlistenemy

Remove object from List after gameObject was destroyed in between Scene loads

Hello! I have been working on a script for mye enemies a while back, which ended up working quite well. Now I have an issue with it, after implementing more scenes and loading these scenes. The scenario when things go wrong:

An enemy is standing within my character's triggerCollider, which makes the enemy appear in my list of enemies. But, when my character also stands inside a portal (stage change event) and I press a button to load a new scene, I will get an error with the list that the enemy in the list can not be found.

So, what I need is a code that can check if my colliderList contains (enemy) and if this enemy was deactivated or destroyed, and remove the enemy from the list accordingly. I've been trying things for myself, but ended up empty. So I hope you can help me putting me in the right direction of the answer!

Here is some code to understand how my code is set up:

     List<Collider2D> colliderList = new List<Collider2D> ();

      [in Update:]
         foreach (Collider2D enemy in colliderList) {
             EnemyHealth eh = (EnemyHealth)enemy.GetComponent ("EnemyHealth");
             if(eh.curhp <= 0) {colliderList.Remove (enemy); return;}

                     //So, here I want something like: if(colliderList.Contains(enemy) 
                                                       && isNotHereAnymore) {colliderList.Remove(enemy);
         }
     //Trigger events for adding an enemy into the collider for MeleeAttack
     void OnTriggerEnter2D (Collider2D col) {
         if (!colliderList.Contains (col) && col.gameObject.tag == "Enemy") {
             colliderList.Add (col);
         }
     }
     
     void OnTriggerExit2D (Collider2D col) {
         if (colliderList.Contains (col) && col.gameObject.tag == "Enemy") {
             colliderList.Remove (col);
 
         }
     }


So, I can understand why it malfunctions, the enemy isn't removed in any way that I have stated that it should be removed, either by its hp being 0, or it exiting the triggerCollider. This is where I shout help. haha!

I hope some of you can bear with me on my ineptitude, cheers.

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
6
Best Answer

Answer by Baste · Dec 15, 2014 at 12:07 AM

You can't call any methods on the enemies from the last scene, or check if they're dead - they're not there any more.

You probably don't need any information from the last scene when you change levels - in that case, just ditch the old stuff in the list of colliders:

 void OnLevelWasLoaded(int level) {
     colliderList.Clear();
 }

Then you have a nice list that's ready for the next level. If you need to keep anything in the list between scene loads, you'll have to remove all empty elements. If this only happens when you load a scene, you put this in OnLevelWasLoaded, if not, you can do it in update.

  colliderList.RemoveAll(item => item == null);

That'll remove ever item in the list that are null, which is what's causing the problem. If you haven't seen the syntax before, look up C# lambdas.

Comment
Add comment · Show 2 · 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 Stealthygolem · Dec 15, 2014 at 01:53 AM 1
Share

I didn't know about the .Clear function. This was pretty nice. So, what I did was just adding this to a coroutine that returns 0, (meaning 1 frame)

 IEnumerator waitFrame() {
     yield return 0;
     this.gameObject.SetActive(false);
 }

This way I have one frame of the object not seeing any enemy before he is setInactive, and there is time to clear the list right before he turns inactive. And when he turns active again, the character is working fine :) Thanks for your help, it helped shed light on the issue! Cheers!

avatar image yotingo · Sep 25, 2017 at 12:49 PM 0
Share

Thanks for the clear and easy-to-use answer! Worked perfectly.

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

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

Related Questions

Possible shader interference when updating matrix 1 Answer

Brute Force Search Algorithm 0 Answers

How to properly create a 2 dimensional array of an object. [C#] 1 Answer

How to fix some of location have more than 1 object? 2 Answers

Instantiate a prefab from jsonarray? 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