• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by blackcloud1971 · Nov 06, 2013 at 12:13 AM · yieldblinking

Blink object on collision

I have a fish where the objective is to avoid Enemy fish. Now when the player fish hits the enemy fish i want the player fish to blink twice (n number if possible)

     void BlinkPlayer(int numBlink)
     {
         for(int i=0; i < numBlink; i++)
         {
             transform.renderer.enabled = false;
             StartCoroutine( Wait (.2f));
             transform.renderer.enabled = true;
             StartCoroutine( Wait (.2f));
         }
     }
     
     IEnumerator Wait(float seconds)
     {
         Debug.Log ("Wait");
         yield return new WaitForSeconds(seconds);    
     }

Then im calling BlinkPlayer() from OnCollisionEnter() Doesn't matter how lrge value I give for Wait() my player fish doesn't blink. I also tried taking out the for() loop inside BlinkPlayer(), but that didn't work out either. Although it does print "Wait" in console.

Any idea what am I doing wrong? I am using unity 4.1 if that matters.

Comment
Add comment · Show 2
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 Huacanacha · Nov 06, 2013 at 12:26 AM 0
Share

I'm a bit hazy on Co-routines so I won;t put this as an Answer, but... I believe that StartCoroutine isn't blocking, so you will immediately disable then enable your renderer (for net zero effect!).

Try putting the enable and disable code in the Wait Co-routine before and after the yield statement, and only calling the co-routine once.

    void BlinkPlayer(int numBlink)
     {
         StartCoroutine( Wait (.2f));
     }
  
     IEnumerator Wait(float seconds)
     {
        Debug.Log ("Wait");
        transform.renderer.enabled = false;
        yield return new WaitForSeconds(seconds); 
        transform.renderer.enabled = true;
     }

If that works add the N parameter to Wait(), add the for loop in there, and rename Wait to Blink or something.

avatar image blackcloud1971 · Nov 06, 2013 at 03:15 AM 0
Share

@Huacanacha: u & rutter posted same answer & both seems to driving me to the right direction. But it didn't totally fix my problem. I am going to comment my problem in answer, just letting u know in case u wanna join

1 Reply

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

Answer by rutter · Nov 06, 2013 at 12:20 AM

You're close, but missing one crucial detail.

Unity's docs say that StartCoroutine returns "immediately". The function making the call doesn't wait. It's only the coroutine itself that waits.

You could probably re-organize it something like this:

 void BlinkPlayer(int numBlinks) {
     StartCoroutine(DoBlinks(numBlinks, 0.2f));
 }
 
 IEnumerator DoBlinks(int numBlinks, float seconds) {
     for (int i=0; i<numBlinks*2; i++) {
     
         //toggle renderer
         renderer.enabled = !renderer.enabled;
         
         //wait for a bit
         yield return new WaitForSeconds(seconds);
     }
     
     //make sure renderer is enabled when we exit
     renderer.enabled = true;
 }



Edit: It occurred to me that it might be easier to control the coroutine by duration (in seconds), as opposed to a fixed number of blinks. Something like this...

 public class Blink : MonoBehaviour {
     void Start () {
         StartCoroutine(DoBlinks(3f, 0.2f));
     }
     
     IEnumerator DoBlinks(float duration, float blinkTime) {
         while (duration > 0f) {
                 duration -= Time.deltaTime;
      
            //toggle renderer
            renderer.enabled = !renderer.enabled;
      
            //wait for a bit
            yield return new WaitForSeconds(blinkTime);
         }
  
         //make sure renderer is enabled when we exit
         renderer.enabled = true;
     }
 }
Comment
Add comment · Show 10 · 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 blackcloud1971 · Nov 06, 2013 at 03:22 AM 0
Share

thanks both of you for the code, you guys are right about the co-routine calling. Right now at least my fish is disappearing-appearing. But the problem is it's NOT blinking. Now the player is getting hidden & after about 1 sec it's re-appearing again. No matter how small value i provide for WaitForSeconds() it does not seem to be working. Please let me know if I should create a new question for this one.

avatar image Huacanacha · Nov 06, 2013 at 03:28 AM 0
Share

Right, then the problem is that you are disabling the whole fish renderer and not just the eyes. You may want to perform an animation ins$$anonymous$$d of hiding components. How exactly do you intend to make the fish blink?

avatar image blackcloud1971 · Nov 06, 2013 at 03:33 AM 0
Share

Yes I actually have a picture of a fish on a Quad as this is a 2D game. I'm disabling the renderer of that quad. And for blinking I want it to appear and disappear couple of times in a very short time

avatar image Huacanacha · Nov 06, 2013 at 03:36 AM 0
Share

You want the whole quad to disappear, or just the texture and have a blank quad displayed? You could swap out or disable the texture rather than turning off the whole renderer, which will just make the whole object disappear.

Or is the problem that it is disappearing for too long a time?

avatar image blackcloud1971 · Nov 06, 2013 at 03:39 AM 0
Share

I actually didn't think about disabling just the texture. I don't $$anonymous$$d doing that because my quad has the same color as background so it should look the same.

And yes the problem is it's disappearing for too long before re-appearing again

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

A delay/yield/wait function for function Update? 6 Answers

Do not delete GUI after 2nd collision 1 Answer

How can I blink a gameobject faster and faster? 1 Answer

Calling IEnumerator function with variables does not do anything 1 Answer

Understanding yield 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