• 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 /
  • Help Room /
avatar image
0
Question by Jan182 · Sep 01, 2015 at 03:48 PM · c#yieldyield waitforseconds

Yield WaitForSeconds doesn't work!

I've got this code here and want it to wait 3 sec, before the check boolean gets reversed, but I'm unable to make this happen, since I've obviously put a mistake in my code which I don't find.

My aim is, to make the text fade out and then in again, endlessly.

 using UnityEngine;
 using System.Collections;
 
 public class BlinkingText : MonoBehaviour {
 
     float duration = 3;
     public GUIText guiText;
     public bool check = false;
 
     void Update () {
         if (check == false) {
             Color myColor = guiText.color;
             float ratio = Time.time / duration;
             myColor.a = Mathf.Lerp (1, 0, ratio);
             guiText.color = myColor;
            Pause ();
             check = !check;
         }
         else if (check == true) {
             Color myColor = guiText.color;
             float ratio = Time.time / duration;
             myColor.a = Mathf.Lerp (0, 1, ratio);
             guiText.color = myColor;
             check = !check;
         }
         
     }
 
     IEnumerator Pause() {
         yield return new WaitForSeconds(3);
     }
 }


Please 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 Scribe · Sep 01, 2015 at 03:52 PM 0
Share

Update cannot be yielded. You could move all your code into something other than Update, and then use InvokeRepeating to achieve the desired effect.

1 Reply

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

Answer by Mikilo · Sep 01, 2015 at 03:54 PM

Hello.

Calling Pause() in Update wont make it run correctly.

Since it is a coroutine, you need to call it through StartCoroutine().

My advice would be to put all your code in a coroutine and call it in Start().

Like that:

     float duration = 3;
     public GUIText guiText;
     public bool check = false;

     void Start()
     {
         StartCoroutine(this.Fade());
     }

     IEnumerator Fade()
     {
         float timeLeft = duration;
         float timePassed = 0F;

         while (true)
         {
             myColor.a = Mathf.PingPong(timePassed, 1F);
             guiText.color = myColor;

             timeLeft -= Time.deltaTime;
             timePassed += Time.deltaTime;

             if (timeLeft < 0F)
             {
                 timeLeft = duration;
                 yield return new WaitForSeconds(duration);
             }
             else
                 yield return null;
         }
     }
Comment
Add comment · Show 9 · 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 Jan182 · Sep 01, 2015 at 04:14 PM 0
Share

thank you very much, it works fine to 99%! :D

the 1% problem is that it doesn't fade anymore, the text gets just appears and disappears...

any ideas?

avatar image Jan182 · Sep 01, 2015 at 04:27 PM 0
Share

sorry, but it still doesn't work :(

avatar image Mikilo · Sep 01, 2015 at 04:47 PM 1
Share

Ok I think I know what you want exactly. XD

avatar image Jan182 · Sep 01, 2015 at 05:06 PM 1
Share

I just had to add

 Color myColor = guiText.color;

in line 16, but hell yeah that's it then! :D

thank you so much!!

avatar image Mikilo · Sep 01, 2015 at 05:12 PM 1
Share

Finally! Facepalm XD

You're welcome!

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

30 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 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

add yield to an already existing project 0 Answers

how to switch between 2 values after 5 seconds using waitforseconds 2 Answers

Nothing after coroutine gets executed 1 Answer

Add an value to a variable after 2 seconds 1 Answer

Trying to make dash energy system... 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