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 Derf321 · Mar 29, 2015 at 08:03 AM · destroyimmediate

DestroyImmediate alternative?

Hello, I'm looking to use the following code to destroy a list of GameObjects, but I also instantiate another list of GameObjects in the same frame with the possibility of having the same tags. I know I could use DestroyImmediate, but the docs recommend against it ESPECIALLY in for loops. I can't use a yield WaitUntilEndOfFrame because it is a static function. Any ideas?

 var gos = GameObject.FindGameObjectsWithTag("Pads");
     for (var j = 0; j < gos.Length ; j++)
          Destroy(gos[j]);
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 Lilbob · Mar 29, 2015 at 04:01 PM 0
Share

What will happen if they have the same name?

avatar image Derf321 · Mar 29, 2015 at 06:32 PM 0
Share

Actually, it's not an issue if they have the same name but rather the same tag. I have a counter in the same frame that will count all objects with the tag and output the number, so if they don't delete first it counts all new GameObjects and the ones that should be deleted.

1 Reply

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

Answer by _joe_ · Mar 29, 2015 at 04:16 PM

The problem here is you using FindGameObjectsWithTag. This is when your newly instantiated objects will most probably have the same tag, and end up being added to the gos variable and being deleted.

The best approach is to use Lists (or arrays since you're using Javascript), loop through the old one delete everything and instante new ones in a new array. That way, you don't really care about naming.

But if you want to keep that approach, the best way is to use coroutines, I wrote you a code in C# (you can use it, or just change it to Javascript), when i start deleting, and only when the deletion call finishes, i start instantiating.

 GameObject gos[];

 IEnumerator Start(){
         gos = GameObject.FindGameObjectsWithTag("Pads");

         //Start and wait for DestroyMyGameObjects to finish
     yield return StartCoroutine("DestroyMyGameObjects");
         
         //This will only be called when the first Coroutine is finished
         InstantiateGameObjects();
 }
 
 //The coroutine
 IEnumerator DestroyMyGameObjects(){
     foreach(GameObject g in gos){
         Destroy(g);
     }
     yield return null;
 
 }
 
 //The function to call when the coroutine is finished
 void InstantiateGameObjects(){
     //Instantiate Code
 }

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 Derf321 · Mar 29, 2015 at 07:25 PM 0
Share

That's exactly what I needed! I guess it was just multiple scripts running at once in the same frame, I just needed to do "yield StartCoroutine" to do them sequentially. It's odd that I'm using $$anonymous$$onoDevelop and it doesn't even give me the option to do yield StartCoroutine, I just tried it and it worked! Cheers!

avatar image _joe_ · Mar 29, 2015 at 07:45 PM 0
Share

You're welcome!

avatar image MineLucky · Dec 20, 2015 at 03:02 AM 0
Share

Really helpful,thank you.

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

23 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

Related Questions

Trouble with Object Pooled Enemy Waves 0 Answers

blasting the current object, if it touches anything 0 Answers

Does UnityEngine.Object.DestroyImmediate() invoke PrefabUtility.PrefabInstanceUpdated? 0 Answers

destroyImmediate on children not working correctly? 6 Answers

Error destroying prefab 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