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 tuzi419711227 · Apr 22, 2021 at 06:50 PM · listdestroyhierarchybool

How to destroy gameobject in hierarchy with a bool list.

Here I have a foreach function that outputs all the gameobject with the "enemy" tag in the hierarchy. Each "enemy" tag gameobject has a static bool candestory. How do I destroy those gameobjects in hierarchy when the bool is true?

foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject))) {

             if(obj.CompareTag("enemy")){

                 Debug.Log(obj.name);

                 list1.Add(obj);
        }




public class Enemy : Character {

public static bool shoulddestroy= false;

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("Player")){
         shoulddestroy = true;
         print("you should destory on next load"+ other.gameObject);
     }
         
 }

 void OnTriggerExit2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("Player")){
         
         print("im out"+ other.gameObject);
     }
         
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by HellsHand · Apr 22, 2021 at 08:20 PM

You shouldn't use public static bool shoulddestroy if there are multiple enemies, since this will cause all instances of the script to share the value, leave off the static. As far as destroying the object you can just add:

 if(shoulddestroy)
 {
     int index = 0;
     foreach(GameObject obj in list1)
     {
         if(obj == gameObject) break;
         else index++;
     }
     list1.RemoveAt(index);
     Destroy(gameObject);
 }  

to your Update() of the Enemy script. This will destroy the object and remove it from the list.

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 tuzi419711227 · Apr 22, 2021 at 09:27 PM 0
Share

Hello, thank you so much for pointing out the static bool. now I remove the static on the bool statement but run into a different issue. How do I avoid the bool shoulddestory being reset after reload the scene. I don't think I can use DontDestroyOnLoad on the Enemy script because it attach to multiple enemies. so right now I have the following on a different script. But the enemy.shoulddestroy reset to false after reload the scene. please help

void Awake() {

      if (Instance == null)

      {

          DontDestroyOnLoad(gameObject);

          Instance = this;

      }

      else if (Instance != this)
      {

          Destroy(gameObject);
          
      }
      
  }

 
  void Start()
  {
       foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
         {     
             if(obj.CompareTag("enemy")){
                 Debug.Log(obj.name);
                 list1.Add(obj);
             }
         }
  }

  
  void Update()
  {
     
     foreach(GameObject obj in list1)
     {
      if(obj == gameObject && enemy.shoulddestroy)
      
         Destroy(gameObject);
     }
avatar image
0

Answer by nursedayuksel · Apr 22, 2021 at 07:19 PM

You can try something like this:

      if (other.gameObject.CompareTag("Player")){
          shoulddestroy = true;
          for (var i = 0; i < list1.Length; i++) {
                 Destroy(list1[i]);
          }
          print("you should destory on next load"+ other.gameObject);
     }




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 tuzi419711227 · Apr 22, 2021 at 08:01 PM 0
Share

This would destroy all enemy gameobject disregard their boolean value, but I think I have an idea.

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

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

Related Questions

A node in a childnode? 1 Answer

C# override a destroyed GameObject in a List 1 Answer

Destroying clone object in list does not remove the first one 0 Answers

Create a button from an int, remove button when clicked, and never load it again 1 Answer

Does Destroy remove all instances? 4 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