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 Kammerman02 · Apr 03, 2015 at 12:20 PM · gameobjectspritewaitforsecondstoggle

How to make a sprite toggle on and off after a few seconds?

Basically what I am trying to do is to gave a sprite that is on my screen to blink on for a few seconds and then blink off, and I want this to happen as a constant loop. I know instead of deactivating the game object itself I should just activate and deactivate the renderer and use wait for seconds for the pauses, but I am unsure how this script is would be formatted. If anybody can help me that would be great. Thank you in advance.

edit I also just wanted to make note that the sprite I am using is through Unity 5`s UI system.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jcv8000 · Apr 03, 2015 at 01:08 PM

 public GameObject theSprite;
 
 void Start()
 {
     StartCoroutine(StartBlinking()); //Doesn't need to be in Start() but use this line wherever you need it.
 }
 
 IEnumerator StartBlinking()
 {
     yield return new WaitForSeconds(1); //However many seconds you want
     theSprite.GetComponent<SpriteRenderer>().enabled = !theSprite.GetComponent<SpriteRenderer>().enabled; //This toggles it
     StartCoroutine(StartBlinking());
 }

In the editor drag the sprite into the "The Sprite" box. Or you could attach this script to the sprite itself and replace "theSprite" in the code with "this".

If you want to stop the blinking, use this:

StopAllCoroutines();
theSprite.GetComponent<SpriteRenderer>().enabled = true;
Since this is stopping all coroutines in the whole script, you should probably keep this in a script by itself or make sure you have no other coroutines in the script.
Comment
Add comment · Show 5 · 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 Kammerman02 · Apr 03, 2015 at 03:05 PM 0
Share

So I tried your script and it didn`t pull any errors, but it will only blink one time. It will only activate if it is currently deactivated or vice versa, it didn`t create the looping effect I was hoping for. What I am trying to achieve is that it flashes onscreen on and off in a constant loop. And again thank you for replying!

avatar image jcv8000 · Apr 03, 2015 at 04:13 PM 0
Share

Oh sorry I did that wrong. Change the IEnumerator StartBlinking to this:

IEnumerator StartBlinking()
{
    yield return new WaitForSeconds(1); //However many seconds you want
    theSprite.GetComponent().enabled = !theSprite.GetComponent().enabled; //This toggles it
    StartCoroutine(StartBlinking());
}
I've tested this and it works :) I'll update the original answer with this.
avatar image jcv8000 · Apr 03, 2015 at 04:38 PM 0
Share

One more edit...

avatar image jcv8000 · Apr 03, 2015 at 08:26 PM 0
Share

One final edit and I have the way to stop blinking working :) Sorry about all that :P

avatar image Kammerman02 · Apr 04, 2015 at 04:51 PM 0
Share

Yes! That worked, thank you for the answer!

avatar image
0

Answer by Notter · Apr 03, 2015 at 01:07 PM

You can use a Coroutine it's basically a method that runs independent of the frame-rate.

Unity describes how to use it in it's documentation: http://docs.unity3d.com/ScriptReference/Coroutine.html

so, at your Start() function, use: StartCoroutine("name"); and your coroutine should disable/enable the sprite renderer and then WaitForSeconds

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 Kammerman02 · Apr 03, 2015 at 03:06 PM 0
Share

Thank you for answering!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Game freezes everything when calling WaitForSeconds 1 Answer

Toggle gameobject on/off with button 3 Answers

Toggle through sprites on UI button press 0 Answers

Instantiate a GameObject at a position specific to an element that was found in a text 1 Answer

Help With Disabling/Enabling single GameObject 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