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 Tanksy · Nov 25, 2013 at 01:19 AM · timewhileblinkingrenderer.enabled

Trying to make a renderer blink on and off, it's not working but I get no errors when building?

Here is my code:

     IEnumerator blinkTime () {
         if (blinking == true) {
             blinkTimeOn = true;
             while (blinkTimeOn == true) {
                 renderer.enabled = false;
                 yield return new WaitForSeconds (1f);
                 blinkTimeOff = true;
                 blinkTimeOn = false;
             }
             
             while (blinkTimeOff == true) {
                 renderer.enabled = true;
                 yield return new WaitForSeconds (1f);
                 blinkTimeOn = true;
                 blinkTimeOff = false;
             }
         }
     }

I'm trying to make an object's renderer be enabled for a second then disabled for a second and this will repeat until blinking is set to false.

What exactly am I doing wrong here?

Edit: I should point out that I have "blinkTime" being called here:

 void OnTriggerEnter(Collider otherObject) {

     if (otherObject.gameObject.CompareTag ("Enemy")) { 
         playerLives--;
         Timer = Time.time;
         blinking = true;
         blinkTime();

         if (playerLives < 1) {
             Destroy(this.gameObject);
         }
     }
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 25, 2013 at 01:22 AM 0
Share

For one thing your logic is way overcomplicated for what you are trying to do. The three blink booleans can all be nuked unless you use their values outside the Coroutine (the code you provided doesn't).

 IEnumerator blinkTime () {
     renderer.enabled = false;
     yield return new WaitForSeconds (1f);
     renderer.enabled = true;
 }

This is basically equivalent to your Coroutine code.

avatar image iwaldrop · Nov 25, 2013 at 01:36 AM 0
Share

Agree with Huacanacha, but wanted to add that there is no reason to have two booleans that always store the opposite values of each other (like blinkTimeOn and blinkTimeOff).

Lets say blinkTimeOn was true, but you wanted something to happen when blinkTimeOn wasn't true. You just use an exclamation point (!) to invert the polarity of the value.

 if (blinkTimeOn)
 {
     // blinkTimeOn is true
 }
 
 if (!blinkTimeOn)
 {
     // blinkTimeOn is false
 }

1 Reply

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

Answer by iwaldrop · Nov 25, 2013 at 01:37 AM

The reason it isn't working is because you're not calling blinkTime via the StartCoroutine method.

 enter code herevoid OnTriggerEnter(Collider otherObject) {
  
     if (otherObject.gameObject.CompareTag ("Enemy")) { 
        playerLives--;
        Timer = Time.time;
        blinking = true;
        blinkTime();
  
        if (playerLives < 1) {
          Destroy(this.gameObject);
        }
     }

Should be:

 void OnTriggerEnter(Collider otherObject) {
  
     if (otherObject.gameObject.CompareTag ("Enemy")) { 
        playerLives--;
        Timer = Time.time;
        blinking = true;
        StartCoroutine(blinkTime());
  
        if (playerLives < 1) {
          Destroy(this.gameObject);
        }
     }
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 Tanksy · Nov 25, 2013 at 09:15 PM 0
Share

Thank you iwaldrop and thank you Huacanacha! I will give these a try and hopefully will get the result I want. Thank you again for the help :)

Edit: Great success. Combining both your answers has given me exactly what I was wanting in the first place!

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

while coroutine not ending correctly using unityscript 1 Answer

start timing when value is reached 1 Answer

Repetitive task within a fixed timeframe? 0 Answers

How to create an agenda or timetable system? 1 Answer

Animation Window: How to scale keyframes to increase time? 9 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