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 DubstepDragon · Jul 24, 2013 at 02:16 PM · erroryieldwaitforseconds

WaitForSeconds...

I have had excruciating tries of finding a solution to this error, but I seem to find nothing. I have a simple cube prefab that spawns, and I want it to be destroyed after a three second delay. I used this:


 using UnityEngine;
 using System.Collections;
 
 public class Cubes : MonoBehaviour {
     
     public float delay = 0.1f;
     public GameObject cube;
     public int destroyTimer = 3;
     
     void Start () {
         InvokeRepeating("Spawn", delay, delay);
         Destruction();
     }
     
     void Spawn () {
         Instantiate(cube, new Vector3(Random.Range(-6, 6), 10, -2), Quaternion.identity);
     }
     
     void Destruction() {
         yield return new WaitForSeconds(destroyTimer);
         Destroy(gameObject);
     }
     
 }


I am getting the same stupid error over and over, no matter what I do. Here it is: "`Assets/Cubes.cs(18,14): error CS1624: The body of Cubes.Destruction()' cannot be an iterator block because void' is not an iterator interface type`"

Can anyone help me out? Thanks in advance!

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

Answer by vividhelix · Jul 24, 2013 at 02:27 PM

Use the variant with a delay.

 Destroy(gameObject, destroyTimer);
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 DubstepDragon · Jul 24, 2013 at 02:28 PM 0
Share

Awesome, thanks a bunch!

avatar image
1

Answer by Hotshot10101 · Jul 24, 2013 at 02:25 PM

yield can only be used inside an Iterator method. An iterator method is best invoked using a corouting.

Try something like this:

 IEnumerator DestroyRouting ()
 {
   yield return new WaitForSeconds (destroyTimer);
   Destroy (gameObject);
 }
 
 void Destruction ()
 {
   StartCorouting (DestroyRouting ());
 }

The above code will not totally solve your particular problem, however, because you have other problems with the way you are instantiating your objects and such. When you Destroy (gameObject) you are basically destroying whatever object the script is on, not the one that got created during Instatiate. I didn't solve that for you ;-).

Having said all that I was basically answering your question about the specific problem you are having. If it were me I would not use the coroutine method at all. Destroy has a time delay parameter right on it. You just have to use: Destroy (gameObject, x) where x is the number of seconds to delay before destroying. In your case it would be Destroy (gameObject, 3);

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 DubstepDragon · Jul 24, 2013 at 02:28 PM 0
Share

That is a possibility, but radlemur's answer is much easier and works. Thanks anyway dude!

avatar image Hotshot10101 · Jul 24, 2013 at 02:30 PM 0
Share

If you look at END of my answer I said the same thing as he did. I was just answering your other question about why you were getting the error and how to use it correctly.

avatar image vividhelix · Jul 24, 2013 at 02:34 PM 0
Share

I missed that too, otherwise I wouldn't have posted the same thing. When I saw the IEnumerator code I stopped reading.

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

18 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

Related Questions

A node in a childnode? 1 Answer

Using Yield Wait for seconds with instantiate 1 Answer

Waiting between pingpong loops 2 Answers

Unity Script Editor Not Working 1 Answer

Set Graphics Emulation to No Emulation as Default? 2 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