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 Doozku · Jul 03, 2015 at 09:06 PM · javascriptcoroutineyield waitforseconds

Yield Never Works

Hello,I've been coding for a bit of time but I have never been able to get yield to work for me. It doesn't work. This is one of the scripts that needs to yield.

 #pragma strict
 
 var VictoryTheme : AudioClip;
 
 
 
 function OnTriggerEnter (info : Collider) 
 {
     if(info.tag == "Player")
     {
         GameMaster.StopAllAudio();
          AudioSource.PlayClipAtPoint(VictoryTheme, transform.position);
          Time.timeScale = 0;
          yield WaitForSeconds(3.5f);
          Time.timeScale = 1;
          GameMaster.CreateMusic = true;
          GameMaster.MusicManager.GetComponent.<AudioSource>().enabled = false;
         Application.LoadLevel ("MainMenuPorted");
     }
 }

The script happens when the player collects and item. It ends the level and sends them back to the menu. I want it to play a victory jingle and then load the next level, but it just yields forever and never ends. Thank you in advance.

Edit: I moved it to a separate function and I called it by start coroutine. But it works better now but now it just skips over the actual yield. Or in other words, it works but it doesn't yield. Here is the new code.

 #pragma strict
 
 var VictoryTheme : AudioClip;
 
 
 function OnTriggerEnter (info : Collider) 
 {
     if(info.tag == "Player")
     {
         StartCoroutine("Stop");
          Debug.Log("Finished the yield");
          Time.timeScale = 1;
          GameMaster.CreateMusic = true;
          GameMaster.MusicManager.GetComponent.<AudioSource>().enabled = false;
         Application.LoadLevel ("MainMenuPorted");
     }
 }
 
 function Stop() 
 {
     GameMaster.StopAllAudio();
      AudioSource.PlayClipAtPoint(VictoryTheme, transform.position);
      Time.timeScale = 0;
      yield WaitForSeconds(VictoryTheme.length); // It skips past this line of code
 }

thank you for the help.

Comment
Add comment · Show 1
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 maccabbe · Jul 03, 2015 at 09:21 PM 0
Share

yield is dependent on Time.timeScale. e.g. if timeScale is at 2x then the time alloted by yield statements gets used twice as fast so 1 second becomes 2 seconds. If you try to set Time.timeScale to 0x then the time alloted by yield statements gets used 0x as fast and will take forever.

4 Replies

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

Answer by Doozku · Jul 04, 2015 at 07:09 PM

I figured it out. maccabbe said that yield is linked to time.timescale. So basically I changed it from 0 to 0.0001 and multiplied the delay by 0.0001. Here is the new code.

 #pragma strict
 
 var VictoryTheme : AudioClip;
 
 
 function OnTriggerEnter (info : Collider) 
 {
     if(info.tag == "Player")
     {
         GameMaster.StopAllAudio();
          AudioSource.PlayClipAtPoint(VictoryTheme, transform.position);
          Time.timeScale = 0.0001;
          yield WaitForSeconds(VictoryTheme.length * 0.0001);
          Debug.Log("Finished the yield");
          Time.timeScale = 1;
          GameMaster.CreateMusic = true;
          GameMaster.MusicManager.GetComponent.<AudioSource>().enabled = false;
         Application.LoadLevel ("MainMenuPorted");
     }
 }

So basically the error didn't have to do with functions or really with the yield.

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 YoungDeveloper · Jul 05, 2015 at 09:18 PM 0
Share

thats an awful solution, create you own global time value for scaling what you need

 private static float time = 1f;
 
 public static void setTime(float value){
    this.time = value;
 }
 
 //use this
 public static float myCustomTime{
     get{ return this.time;}
 }
avatar image Doozku · Jul 07, 2015 at 12:11 AM 0
Share

Umm... This is unityscript, not C#

avatar image
1

Answer by Dave-Carlile · Jul 03, 2015 at 09:22 PM

Yield only works in a coroutine. The Coroutine documentation has a decent example and description of how they work.

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 YoungDeveloper · Jul 03, 2015 at 09:15 PM

You can't use yield in trigger, it can't be an IEnumerator. Create separate function which will handle the yield and launch it.

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 maccabbe · Jul 03, 2015 at 09:28 PM 0
Share

It seems like you can use yield in trigger (in Javascript). I just tested the following code

 #pragma strict
  
 function OnTriggerEnter (info : Collider) 
 {
     Debug.Log(0);
     yield WaitForSeconds(3);
     Debug.Log(1);
 }

in a scene with 2 cubes each with istrigger set to true and a rigidbody that didn't use gravity and was kinematic and it worked as expected.

avatar image YoungDeveloper · Jul 03, 2015 at 10:23 PM 0
Share

This is how it should be done http://answers.unity3d.com/questions/172055/help-with-yield-wait-for-seconds-.html

Personally i dont use unityscript as i find it extremely unreadable. You should specify the type in my opinion.

 function $$anonymous$$yWaitFunction():IEnumerator{
 
 }

New users dont understand the idea behind this just for this reason.

avatar image
0

Answer by MCoburn · Jul 07, 2015 at 12:44 AM

Like what Dave Carlile said above, what I'd do is this:

When player achieves the goal (whatever that may be), then fire a coroutine that may be "LevelEnd". Tell the game not to accept any input, etc so player can't mess around while the coroutine is running. The coroutine does its job, saves score/life/etc to disk and then yields X seconds before finally saying "Unity, load this level".

Yield will not work in voids. C# will throw an error should you try to do so. Also, if you're using C#, you may need to do this:

return yield WaitForSeconds(1.5f);

I also highly recommend switching to C# if you haven't done so, UnityScript is moreso "legacy" and while it'll be around for some time longer, C# is in my books, cleaner and more powerful.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Yield Instruction for FixedUpdate frames 3 Answers

StartCoroutine gets the arglist type wrong 1 Answer

Coroutine error help 0 Answers

Problem with yield WaitForSeconds (I think) -javascript) 1 Answer

CoRoutione from FixedUpdate not working 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