Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by KnightRiderGuy · Dec 15, 2015 at 04:02 PM · c#uiui imageblinkingblink

how Can I make a UI Image Blink On And Off

I'm trying to make a UI turn signal indicator using a UI image that I want to blink on and off with a click, click sound. I can do it with an animation but is there a C# method that is a better way to do this so that when the game object is set active it will be blinking and making the classic "Click, Click" sound of a turn signal indicator?

Comment
Add comment
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

4 Replies

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

Answer by wilmer_lin · Dec 15, 2015 at 10:07 PM

I did something similar and made a Blink.cs class. Then attached it to whatever UI object I wanted to blink. Change the timing with the interval, etc. In this example, I just used AudioSource.PlayClipAtPoint to play your specified AudioClip at world origin.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 // this toggles a component (usually an Image or Renderer) on and off for an interval to simulate a blinking effect
 public class Blink : MonoBehaviour {
 
     // this is the UI.Text or other UI element you want to toggle
     public MaskableGraphic imageToToggle;
 
     public float interval = 1f;
     public float startDelay = 0.5f;
     public bool currentState = true;
     public bool defaultState = true;
     bool isBlinking = false;
 
     public AudioClip clip;
 
     void Start()
     {
         imageToToggle.enabled = defaultState;
         StartBlink();
     }
 
     public void StartBlink()
     {
         // do not invoke the blink twice - needed if you need to start the blink from an external object
         if (isBlinking)
             return;
 
         if (imageToToggle !=null)
         {
             isBlinking = true;
             InvokeRepeating("ToggleState", startDelay, interval);
         }
     }
 
     public void ToggleState()
     {
         imageToToggle.enabled = !imageToToggle.enabled;
 
         // plays the clip at (0,0,0)
         if (clip)
             AudioSource.PlayClipAtPoint(clip,Vector3.zero);
     }
     
 }
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 joseGuate97 · Apr 30, 2020 at 07:04 PM 0
Share

Wondering, isn't this missing a way to stop the blinking?

avatar image AryanJumani joseGuate97 · Apr 30, 2020 at 09:38 PM 0
Share

@joseGuate97 the way I would have done this was use a update void where if whichever button pressed, blink.enabled = false; This code wont allow the blinking to start again but it can be repaired by usages of bools

avatar image
5

Answer by RadioKnife · Dec 15, 2015 at 10:02 PM

You can use Coroutine to achieve this. It would go something like this

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class BlinkingImage : MonoBehaviour {
 
     Image image;
 
     void Start ()
     {
         image = GetComponent<Image>();
         StartBlinking();
     }
 
 
     IEnumerator Blink()
     {
         while (true)
         {
             switch(image.color.a.ToString())
             {
                 case "0":
                     image.color = new Color(image.color.r, image.color.g, image.color.b, 1);
                     //Play sound
                     yield return new WaitForSeconds(0.5f);
                     break;
                 case "1":
                     image.color = new Color(image.color.r, image.color.g, image.color.b, 0);
                     //Play sound
                     yield return new WaitForSeconds(0.5f);
                     break;
             }
         }
     }
 
     void StartBlinking()
     {
         StopAllCoroutines();
         StartCoroutine("Blink");
     }
 
     void StopBlinking()
     {
         StopAllCoroutines();
     }
     
 }
 


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 AnnaB417 · Apr 28, 2016 at 03:04 PM 0
Share

Just a comment: I tried both methods, and prefer the first answer for the following reasons:

I have other Coroutines in my code, so I changed the above "StopAllCoroutines()" to "StopCoroutine(myblink)" only, so the others can still run.

If you're flashing multiple images, the IEnumerator doesn't handle the ti$$anonymous$$g properly, such that the first image listed will flash correctly, but since it's a constant loop the other two don't pick up the ti$$anonymous$$g. I'm pretty new to C# so I may be doing something that's not the most efficient. But the first answer works like a charm!!!

avatar image
1

Answer by LordDarkon76 · Dec 15, 2015 at 09:59 PM

You can start a Coroutine, that toggle the alpha of your object and reproduce the sound then wait the time that you need.

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 KnightRiderGuy · Dec 16, 2015 at 03:26 PM

Thanks @wilmer_lin and @RadioKnife Both of these were excellent answers and I loved them both. I ultimately could only choose one answer as the correct one even though they are both correct and both work. I ended up choosing the top answer simply because of the ease of use with the inspector values of being able to drag and drop and tweak parameters in the inspector.

I awarded points to both of you though because such help was well deserved on both parts, Much MUCH thanks guys :)

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

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

Destroy UI Image 1 Answer

Sprite image not showing up when assigned to Texture2D 0 Answers

Health Bar Messages 3 Answers

2D Image not appearing in game but only in scene 1 Answer

Unity to Check Arduino LED Status 0 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