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 Breakend · Apr 05, 2013 at 02:33 AM · coroutineyieldwaitforsecondsloadlevel

Yield WaitForSeconds doesn't work, give syntax error

Hi,

I am at a bit of an impasse. I have tried everything, but it would seem my Yield WaitForSeconds is not properly formatted, even though I have followed numerous examples. The MonoDevelop and Unity is telling me that there should not be a ( at the WaitForSeconds line. Could someone please let me know what I am doing wrong? Here is my code (I commented out what I had previously tried with creating a coroutine and then waiting a second but the load level did not get called):

     void OnTriggerEnter(Collider otherObject){
         if(otherObject.tag == "enemy"){
             Enemy enemy = (Enemy) otherObject.gameObject.GetComponent("Enemy");
             Instantiate(ExplosionPrefab, enemy.transform.position, enemy.transform.rotation);
             enemy.SetPositionAndSpeed();
             Destroy(gameObject);
             Player.Score += 100;
             
             if(Player.Score >= 1000){
                 Debug.Log ("In 1000");
                 yield WaitForSeconds(1f);
                 Application.LoadLevel(3);
                 //StartCoroutine(WinScreen ());
             }
         }
     }
     
     IEnumerator WinScreen(){
         yield return new WaitForSeconds(1);
         Application.LoadLevel(3);
         Debug.Log ("In coroutine");
     }
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

1 Reply

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

Answer by EliteMossy · Apr 05, 2013 at 02:44 AM

You can't use a yield outside of a IEnumerator.

So what you had originally with the StartCoroutine was correct, the reason it did not run, is you destroyed the gameObject. So nothing below that would work.

Here is a fixed way of doing it.

 void OnTriggerEnter(Collider otherObject){
        if(otherObject.tag == "enemy"){
          Enemy enemy = (Enemy) otherObject.gameObject.GetComponent("Enemy");
          Instantiate(ExplosionPrefab, enemy.transform.position, enemy.transform.rotation);
          enemy.SetPositionAndSpeed();
          Player.Score += 100;
  
          if(Player.Score >= 1000){
           Debug.Log ("In 1000");
           StartCoroutine(WinScreen ());
          }
          else {
            Destroy(gameObject);
          }
        }
     }
  
     IEnumerator WinScreen(){
        yield return new WaitForSeconds(1f);
        Application.LoadLevel(3);
        Destroy(gameObject);
        Debug.Log ("In coroutine");
     }
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 Breakend · Apr 05, 2013 at 03:29 AM 0
Share

EDIT: Sorry, you were right, just not in this place. I destroy the object when it exits the frame and since in this instance it keeps going it still got destroyed before the coroutine was finished. Thanks a lot!

avatar image EliteMossy · Apr 05, 2013 at 03:32 AM 0
Share

Are you destroying the gameObject anywhere else?

 IEnumerator WinScreen(){
        Debug.Log ("In coroutine");
        yield return new WaitForSeconds(1f);
        Debug.Log("WaitForSeconds finished, Loading Level");
        Application.LoadLevel(3);
        Destroy(gameObject);
     }

See if it prints them two lines.

avatar image EliteMossy · Apr 05, 2013 at 03:46 AM 0
Share

@Breakend no problem :)

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

11 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

Related Questions

Wait For Seconds to Load level C# 2 Answers

Waypoint / Yield help 1 Answer

Script gets stuck on WaitForSeconds() 1 Answer

Another yield not working problem 2 Answers

Understanding yield inside a for or a while loop 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