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 Sausagesauce · Aug 07, 2013 at 05:21 PM · javascriptvariablebooleanyieldyield waitforseconds

Yield Waitforseconds not working at all

Yield just WON'T WORK. I've tried everything, and I don't understand it. Unity just refuses to wait. What I'm trying to do is I want an object to move upwards after 8 seconds have passed. Here's my javascript code:

 //Whether it's waited or not
 var dontchangethis : boolean;
 //Set the variable to false
 dontchangethis=false;
 //Wait 8 Seconds
 yield WaitForSeconds (8);
 //Set the 
 dontchangethis=true;
 function Update(){
     if (dontchangethis){
         transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
     }
 }

It won't set the variable to true, because it doesn't wait! What's wrong?

EDIT: I made some changes to the code, with coroutines in mind. Sadly, it still doesn't work: Here's my code again:

 //Whether it's waited or not
 var dontchangethis : boolean = false;
 //Wait 8 Seconds
 function Start(){
     yield StartCoroutine(Wait());
 }
 function Update(){
     if (dontchangethis == true){
         transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
     }
 }
 function Wait(){
     yield WaitForSeconds(1);
     dontchangethis=true;
 }

ANOTHER EDIT: Okay, this is wierd. I imported the script into a new project and it worked, but in the old one it doesn't! So I guess the code works but I have to find the new problem.

Comment
Add comment · Show 4
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 ivan2532 · Aug 07, 2013 at 05:23 PM 0
Share

I can't declear what your code do, tell me.

avatar image perchik · Aug 07, 2013 at 05:56 PM 0
Share

Here's some tips.

Use Debug.Log() to print out stuff to help you debug. I'd do something like this:

 var dontchangethis : boolean; //this is a global variable
 
 function Start(){
     //Set the variable to false
     dontchangethis=false;
     
     Debug.Log("Before yield");
     yield WaitForSeconds(8);//Wait 8 Seconds
     Debug.Log("After yield");
     //Set the variable to true
     dontchangethis=true;
 }
 
 
  
 function Update() {
     if (dontchangethis){    //If the variable's true, then move upwards
        transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
     }
 }

Then when you click play, you'll see in the console "Before yield". 8 seconds later, you'll see "After yield". Also, this code works exactly like expected, when it's on a cube, after 8 seconds the cube launches.

avatar image perchik · Aug 07, 2013 at 05:59 PM 0
Share

In response to your edit, you should do:

 StartCoroutine(Wait());

Ins$$anonymous$$d of

yield StartCoroutine(Wait());

avatar image clunk47 · Aug 13, 2013 at 02:07 AM 1
Share

Please let us know if this issue has been resolved. If it has been, and one of these answers helped, please be so kind to accept and vote up the answer that best provides a proper solution.

3 Replies

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

Answer by clunk47 · Aug 07, 2013 at 05:54 PM

It's working fine on my end. All I needed to do to check this was send the event to Debug Log. I usually put things like this in Start or Awake, even though you don't HAVE to in JS, it just seems more efficient to me.

 var dontchangethis : boolean = false;
 
 function Start()
 {
     yield WaitForSeconds (3);
     dontchangethis=true;
 }
 
 function Update()
 {
     if (dontchangethis)
     {
         Debug.Log(dontchangethis);
         transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
     }
 }
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
1

Answer by creighcl · Aug 07, 2013 at 05:41 PM

It's possible you have provided an incomplete example of what you're trying to do.

In case it isn't what you've done, be sure that your YIELD statements are inside of a Coroutine.

Don't attempt to use it elsewhere, and certainly not within the class script as it appears you are doing.

You'll want to call your Coroutine and that's where you will do your YIELD.

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

hope this helps :-)

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 perchik · Aug 07, 2013 at 05:53 PM 1
Share

In javascript, you can actually do it anywhere. It's dirty, nasty and makes me sad, but you can.

avatar image
1

Answer by perchik · Aug 07, 2013 at 06:08 PM

Since the code works somewhere else, is there any chance that you are stopping time somewhere in your current project, by setting Time.timeScale = 0 ?

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 Sausagesauce · Aug 07, 2013 at 09:42 PM 0
Share

I doubt it, because i've tried putting it into a new scene with nothing in it but the camera, and a cube with that script.

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

Static Variable Problem 1 Answer

Function with itself called inside it. (JavaScript) 2 Answers

Yield WaitForTrue? 1 Answer

Change Variable on Another Script 2 Answers

yield WaitForSeconds(5); IS WAITING FOR EVER 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