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 dcolwell634 · May 27, 2013 at 11:43 PM · gameobjectlistdestroy

How to destroy game objects from a list

Hi all,

Please note my code is in C#

I have a question on destroying game objects. Ok I'll try to be as descriptive as possible. I have a script attached to my player that spawns enemies while the player is active. I have a collider set up so when my player collides with an enemy, the enemy is destroyed. My problem is when I destroy the enemy it disappears but still exists within the game. (doesn't fully destroy) If I call destroy to the actual game object (i.e. on the script attached to my enemy, if I say): if (health <=0) Destroy(gameObject); - all current instances of the enemy are destroyed, and not just the one I hit. I'm wondering what the best way to go about this is. Someone told me to track each enemy in a list and destroy each enemy at the stored place in the list. I.e. (psudocode) foreach (enemy i in list) destroy enemy (i); - but I can't find a way to make this code work. Here is how I add my enemies to my list as well as the code behind my spawn function:

 public class MyClass : MonoBehaviour
 
 {
 
 public GameObject enemyPrefab;
 
 public int maxShips = 1;
 
 List testEnemy;
 
 //EnemyScript is the script attached to my enemy. It makes it move, stores health for the enemy, etc.
 
 EnemyScript enemyScript = new EnemyScript();
 
 
     void Start () 
     {
         testEnemy = new List();
 }
 
 //I have a timer that fires off every so often and I call this when the timer ticks - I don't include the timer code here.
 
     public void SpawnEnemy()
     {    
         for (int i = 0; i < maxShips; i++)
         {
             testEnemy.Add((GameObject)Instantiate(enemyPrefab, new Vector3(boundsRight.x, (Random.Range(boundsTop.y - 1.0f, boundsBottom.y + 0.5f)), 7), Quaternion.identity));
             
         }
     }
 
 //Here is my collider
 
     void OnTriggerEnter(Collider otherObject)
     {
         if (otherObject.tag == "Enemy")
         {
             Destroy(this.enemyScript.gameObject);
             testEnemy.Remove(enemyScript.gameObject);
             
         }
     }
 }

so if I collide with an enemy, the enemy disappears - but isn't actually destroyed. This is an issue because I'm trying to track how many enemies the player was not able to destroy. If an enemy makes it past the screen, I increment this amount by 1. So if I hit the enemy it disappears and to the player it looks as if it was destroyed, but it is still in motion and will eventually pass off the screen (because technically it still exists) - thus skewing my results. Can anyone give me a hand?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by hiddenspring81 · May 28, 2013 at 12:15 AM

Try this instead. I haven't tested this code, so let me know if there are any problems.

 public class MyClass : MonoBehaviour
 {
   public GameObject enemyPrefab;
   public int maxShips = 1;
   List<GameObject> enemies;
  
   void Start () 
   {
     enemies = new List<GameObject>();
   }
  
   public void SpawnEnemy()
   {  
     for (int i = 0; i < maxShips; i++)
     {
       enemies.Add((GameObject)Instantiate(enemyPrefab, new Vector3(boundsRight.x, (Random.Range(boundsTop.y - 1.0f, boundsBottom.y + 0.5f)), 7), Quaternion.identity));
      }
   }
  
   void OnTriggerEnter(Collider otherObject)
   {
     if (otherObject.tag == "Enemy")
     {
       Destroy(otherObject);
       enemies.Remove(otherObject.gameObject);
     }
   }
 }
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
avatar image
0

Answer by dcolwell634 · Jul 20, 2013 at 04:27 AM

thanks @hiddenspring81 for your answer. And sorry for the late response. This is what I was looking to do at the time, BUT I found out an easier was to accomplish it. I just had the enemy ships destroy themselves when they were hit. Then if they pass the screen, it means I didn't shoot them and increment missed integer. I think it didn't work because when I called the destroy game object from this script, instead of the enemy script, it was just destroying the first gameobject in the enemy ship queue and not all objects. So it still had a transform.

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

Answer by Taomujw · Jan 22, 2016 at 01:39 PM

you can make a function with destroy(gameObject) and sendmessage to the object through the list to destroy it.

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

15 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Store Game Object Into List For Later Reinstantiation? 0 Answers

Store a Copy then Destroy Scene Objects 1 Answer

A node in a childnode? 1 Answer

Deleting objects from list sequence 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