• 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
4
Question by KiethFurry · May 21, 2016 at 05:40 AM · spriterenderer

Make a 2D sprite flash?

I'm trying to get my player to flash invisible and visible while it's invulnerable after being hit. Presently, the player is hit and becomes invulnerable for a short time as intended, however it just becomes invisible for as long as it is invulnerable. I wanted to know if there was a way to make a bool, in this instance an "IsVisible" bool, that would toggle on and off every X amount of seconds while another bool is true. Basically, the "IsVisible" would toggle on and cause the sprite to render, toggle off and cause the sprite to not render, on an interval so that it flashed rendered and not rendered. All help would be appreciated. I'm trying to polish up my game for showcase on Sunday. Thanks.

     if (invulnerable)
     {
         player.GetComponent<SpriteRenderer>().enabled = false;
     }
     else
     {
         player.GetComponent<SpriteRenderer>().enabled = true;
     }
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 Amanna · May 23, 2016 at 11:41 AM 0
Share

Are u calling flash () function inside update ? And print (invulneable ) to debug it properly .

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Amanna · May 21, 2016 at 06:28 AM

     public int delay =100;
     public SpriteRenderer mySpriteRenderer;
     int counter;
     bool toggle=false;
     
     void Update()    // you can you FixedUpdate for fixed flash rate
     {
         Flash(mySpriteRenderer);
     }
     
     void Flash(SpriteRenderer spriteRen)
     {
         
         
         if(counter>=delay)
         { 
             counter = 0;
             
             toggle=!toggle;

             if(toggle)
             {
                  spriteRen.enabled=true;
             }
             else
             {
                 spriteRen.enabled=false;
             }
             
         }
         else
         {
             counter++;
         }
     }
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 Amanna · May 21, 2016 at 07:04 AM 0
Share

smaller delay will make flash effect faster.

avatar image KiethFurry · May 21, 2016 at 07:58 PM 0
Share

Thank you. I'll test that once I have another issue figured out. I didn't change a thing in my game and yet every time I load it up now it has horrible frame rate issues. I tried putting in code to set the targeted frame rate, but this didn't resolve the issue. I greatly appreciate the help with the flashing and I really want to test it, but that's looking kind of tough with the game lagging the way it is. It's a 2D game that's never lagged before, and I even closed it and restarted my laptop.

avatar image KiethFurry · May 21, 2016 at 08:10 PM 0
Share

Also I just tested this code you gave me, and the player did not flash. The player remained completely unchanged visual-wise,

if (invulnerable) { Flash(mySpriteRenderer); }

avatar image KiethFurry · May 21, 2016 at 08:19 PM 0
Share

Actually just fixed the frame rate issue randomly. Not sure what fixed it. But either way the code you gave me hasn't worked, although that could just be me not putting it in the correct place.

avatar image Amanna · May 23, 2016 at 11:45 AM 0
Share

Because Flash() func works perfectly , just call it properly .. I don't know the flow of your source code.

avatar image
1

Answer by rdougla7 · Nov 17, 2016 at 07:19 AM

@KiethFurry I know this is old, but, this would probably work.

This is what I referenced.

In the unity editor: Have a collider (box2d,circle2d,poly2d,whatver) on your player, and whatever is hitting your player. Set them both as triggers. If you have multiple objects that you want to have different effects on the player, you can use a tag. Otherwise disregard my "if(other.gameobject.tag)" statement. Note that in my experience using this method, if an untagged object hits your player, the game breaks.

Script Explanation: Make a variable for the players spriterenderer, and it's collider. Get those components in your start function so you're actually referencing something. Use "void OnTriggerEnter2D" to look for the collision. Collider2D Other is what is hitting the player. This is where you can look for tags or not. You would then call the "IEnumerator Flash()". To call you have to use "StartCorutine(information)".

Flash() Explanation: Use the loop to determine how many times you want your player to flash. The time between each flash is determined by "x". "x" is passed into the function when it's called as "flashSpeed", which can be set from editor for tweaking.

 public float flashSpeed;

 PolygonCollider2D polyColl;
 SpriteRenderer spRndrer;

 void Start() {
     polyColl = GetComponent<PolygonCollider2D>();
     spRndrer = GetComponent<SpriteRenderer>();
 }

 void OnTriggerEnter2D(Collider2D other) {
     if (other.gameObject.tag == "objectThatHitsMeTag")
         StartCoroutine(Flash(flashSpeed));
 }


 IEnumerator Flash(float x) {
     polyColl.enabled = false;

     for (int i = 0; i < 10; i++) {
         spRndrer.enabled = false;
         yield return new WaitForSeconds(x);
         spRndrer.enabled = true;
         yield return new WaitForSeconds(x);
     }


     polyColl.enabled = true;
 }


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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing FilterMode possible on a 2D sprite? 1 Answer

How do I enable backface culling for sprites? 2 Answers

Use result from first shader pass in second pass 0 Answers

How to get the spriterender default material in the code 1 Answer

Changing a prefab's sprite doesnt work on mobile, but it does on editor, how s it possibble ? 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