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 /
avatar image
0
Question by Nicholander · Mar 28, 2016 at 12:56 AM · c#programmingwaitforsecondswait

Cant Make C# Use waitforseconds Command

I'm trying to make a little game based off the Roll-A-Ball tutorial game, and I want to make it so that once you've collected all the cubes, the game waits 4 seconds and then starts the next scene/level. However, the waitforseconds command just refuses to work. This is the code I'm using:

 void SetCountText ()
 {
     countText.text = "Count: " + count.ToString();
     if (count >= 12)
     {
         winText.text = "YOU WIN!";
         winSound.GetComponent<AudioSource>().Play ();
         yield return new WaitForSeconds(4);
         SceneManager.LoadScene("Level2");
     }
 }

When I run the debug it says that 'void is not an iterator block', which I don't know what it means. The Scripting API appears to say that the 'IEnumerator' command has to be in place of the void command, but when I do that it doesn't work at all either. Some other questions I've found say that you have to use a 'Coroutine' command, which the Scripting API doesn't say anything useful about other than having the 'IEnumerator' in the example code. Why can't I just use the WaitForSeconds command normally?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gjf · Mar 27, 2016 at 09:36 PM

IEnumerator is not a command, it's a type - if you want to use WaitForSeconds() then you have to call it as a coroutine. change the function to:

 IEnumerator SetCountText()

and call it with

 StartCoroutine(SetCountText());



Comment
Add comment · Show 2 · 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 Nicholander · Mar 27, 2016 at 09:58 PM 0
Share

I tried it, but it didn't work.

I changed it to this:

     IEnumerator SetCountText()
     {
         StartCoroutine(SetCountText());
         countText.text = "Count: " + count.ToString();
         if (count >= 12)
         {
             winText.text = "YOU WIN!";
             winSound.GetComponent<AudioSource>().Play ();
             yield return new WaitForSeconds(4);
             Scene$$anonymous$$anager.LoadScene("Level2");
         }
     }

It also broke that 'section' (For lack of a better word.) of the whole script, meaning that it didn't display how many cubes the player collected and didn't show the 'you win' text after 12 of them are collected. Am I doing it wrong?

Also, is there some explanation of C# syntax somewhere? Like what exactly a 'type' is and what 'calling' means?

avatar image Fredex8 Nicholander · Mar 28, 2016 at 02:18 AM 1
Share

You don't want to start the coroutine from within the coroutine. If SetCountText is called from elsewhere that means every time it starts it is going to try and start another instance of itself and go into an endless loop. If it isn't called from elsewhere first then it is just never going to start.

Remove StartCoroutine(SetCountText()); and use that outside of the IEnumerator wherever it is that you want to start the coroutine from ie:

 void Start()
 {
      StartCoroutine(SetCountText());
 }
 
  IEnumerator SetCountText()
  {
      countText.text = "Count: " + count.ToString();
      if (count >= 12)
      {
          winText.text = "YOU WIN!";
          winSound.GetComponent<AudioSource>().Play ();
          yield return new WaitForSeconds(4);
          Scene$$anonymous$$anager.LoadScene("Level2");
      }
  }



avatar image
0

Answer by sunderplugs11 · Mar 28, 2016 at 05:13 AM

You really need to watch this

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

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 Bleackk · Mar 28, 2016 at 03:01 PM

      IEnumerator changeDelay ()
      {
          SetCountText ();
          yield return new WaitForSeconds (10);
          SceneManager.LoadScene ("Level2");
      }
  
      void SetCountText()
      {
          countText.text = "Score: " + count.ToString();
          if (count >= 1 && !counting)
          {
              counting = true;
              winText.text = "Good Job";
          }
      }
  }

Your whole concept of the code was wrong from the start. What you have to do is start a Corouting of any name in this case it " changeDelay" Get a Void SetCountText put in there the whole code with the score and win text then in the IEnumerator you need to put in the whole Function, THEN wait for 10 seconds and change the whole level. If this does not work then something else in your code is wrong you might want to put in the whole thing.

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Pause a script completely 3 Answers

Wait in seconds before damaging the player again 2 Answers

Problem getting AI to wait 1 Answer

Distribute terrain in zones 3 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