Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Hed94 · Oct 14, 2017 at 01:03 PM · destroywaitforsecondsienumeratorforeach

Foreach skips IEnumerator method - Destroying objects after time

Hi, I have object that contain 6 cubes in a row as his children. I want to slowly destroy them one by one after some time, but my code doesnt seem to work. It totaly skips the Clear Method and continues in the foreach loop. Any advices pls ? :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class podlahaScript : MonoBehaviour
 {
 
     void Start ()
     {
         foreach (Transform child in transform)
         {
             GameObject novy = child.gameObject;
             Clear(novy);
         }
     }
 
     IEnumerator Clear(GameObject novy)
     {
         yield return new WaitForSeconds(1);
         Destroy(novy);
     }
 }
 
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
Best Answer

Answer by NoseKills · Oct 14, 2017 at 04:32 PM

Your Clear method returns an IEnumerator object. Seems like you don't have a clear idea of what that means.

IEnumerator is a thing that can be used to read the data of a collection.

Initially, the enumerator is positioned before the first element in the collection. You must call the MoveNext method to advance the enumerator to the first element

Without necessarily knowing it, you take advantage of this for example every time you use a foreach loop.

There is nothing magical or Unity specific about IEnumerators. You can use a method that returns an IEnumerator like this for example

 IEnumerator Numbers() {
      yield return 1;
      yield return 2;
      yield return 3;
 }
  ...
 IEnumerator num = Numbers();
 while (num.MoveNext()) {
      Debug.Log(num.Current.ToString());
 }
   
 // prints out 1 2 3

You don't call MoveNext() on your enumerator so it never advances past the first yield. Furthermore WaitForSeconds doesn't do anything by itself.

But when you start a coroutine and give it an IEnumerator that returns a WaitForSeconds, the coroutine uses this returned object as an instruction to call MoveNext after that delay.

You should be able to fix your code by changing

 int i = 1;
 foreach (Transform child in transform)
 {
      GameObject novy = child.gameObject;
      StartCoroutine(Clear(novy), i++);
 }
  
 IEnumerator Clear(GameObject novy, float delay)
  {
      yield return new WaitForSeconds(delay);
      Destroy(novy);
  }

If the clear method only destroys the object, you could just use the overload of Destroy() that takes a second parameter (delay)

 int i = 1;
 foreach (Transform child in transform)
 {
      GameObject novy = child.gameObject;
      Destroy(novy, i++);
 }


In general you just have to be careful not to remove or add objects to the IEnumerable (transform) while the foreach is still running on it. That will throw an error. Can't remember if Destroy() without a delay will cause it since it's not instant anyways. If you set a delay, it won't.

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 farmland · Oct 14, 2017 at 03:55 PM

This should help:

 void Start()
 {
     StartCoroutine(Clear(transform));
 }
 
 IEnumerator Clear(Transform novy)
 {
     foreach(Transform child in novy){
         yield return new WaitForSeconds(1);
         Destroy(child.gameObject);
     }
 }

There two problems with your code:

  1. Coroutine is not called as it should be called (StartCoroutine..).

  2. Every time you call a coroutine, you make it wait for 1 second before Destroying the object. But there is no delay between each individual call. So all coroutines are called instantly one after other, they wait for 1 second (together) and after that start deleting the respective gameobjects (almost together at same time).

P.S. : This code is not tested. But should work.

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 Hed94 · Oct 16, 2017 at 11:24 PM 0
Share

Thank for your respond, your code works strangely - I have 6 cubes in a row named 1,2...6 and it destroys 1,2,4,6 :)

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

75 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

Related Questions

How to skip WaitForSeconds? 1 Answer

Using foreach to remove and delete bullet in List - C# 3 Answers

IEnumerator not working.WaitForSeconds never continiues. 2 Answers

Some coding help needed 1 Answer

How to add multiple yield return new wait for seconds inside an IEnumerator? 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