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
0
Question by monopoly10 · Dec 02, 2015 at 10:10 AM · c#unity5sprites

Sprite Fade In/Out Not Working

I'm new to Unity and C#. I'm trying to make a script where a 2D sprite will fade in and remain onscreen for a certain number of seconds, then fade out and stay faded out for a certain number of seconds, and then fade back in and repeat the cycle. I found some info about fades in the forums and tried to modify it to do what I want. But when I play my game, the sprite fades in as planned, then stays there for the right amount of time, and then disappears without fading. It then stays out for the right amount of time and then instead of fading back in, it just appears without fading. The cycle then continues onward like that; the fades stop working after the first initial fade in. Here's what I have for my script:

 using UnityEngine;
 using System.Collections;
 
 public class FadeInOut : MonoBehaviour {
 
     public float minimum = 0f;
     public float maximum = 1f;
     public float durationIn = 1.5f;
     public float durationOut = 3.0f;
     private float startTime;
     private int fadedIn;
     
     public SpriteRenderer sprite;
     
     void Start() {
         sprite.color = new Color (1f, 1f, 1f, 0f);
         fadedIn = 0;
     }
 
     void Update() {
 
         if (fadedIn == 0) {
             float t = (Time.time - startTime) / durationIn;
             sprite.color = new Color (1f, 1f, 1f, Mathf.SmoothStep (minimum, maximum, t));
             StartCoroutine("Wait1");
         }
         
         if (fadedIn == 1) {
             float t = (Time.time - startTime) / durationOut;
             sprite.color = new Color (1f, 1f, 1f, Mathf.SmoothStep (maximum, minimum, t));
             StartCoroutine("Wait2");
         }
     }
 
     IEnumerator Wait1() {
         yield return new WaitForSeconds (5);
         fadedIn = 1;
     }
 
     IEnumerator Wait2() {
         yield return new WaitForSeconds (5);
         fadedIn = 0;
     }
 }

Any help with this would be greatly appreciated!

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

1 Reply

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

Answer by OncaLupe · Dec 04, 2015 at 03:32 AM

The problem is you're not setting the startTime variable, so each time after the first the fade calculation instantly hits the end. A quick fix would be to set startTime = 0f in the coroutines just after the yields.

However, the way the script is set up, the calculation and color setting is being done every frame, even when there is no actual change. Personally I'd use a single coroutine to handle everything like this:

 using UnityEngine;
 using System.Collections;
 
 public class FadeInOut : MonoBehaviour
 {
     public SpriteRenderer sprite;
 
     public Color spriteColor = Color.white;
     public float fadeInTime = 1.5f;
     public float fadeOutTime = 3f;
     public float delayToFadeOut = 5f;
     public float delayToFadeIn = 5f;
 
     void Start()
     {
         StartCoroutine("FadeCycle");
     }
 
     IEnumerator FadeCycle()
     {
         float fade = 0f;
         float startTime;
         while(true)
         {
             startTime = Time.time;
             while(fade < 1f)
             {
                 fade = Mathf.Lerp(0f, 1f, (Time.time - startTime) / fadeInTime);
                 spriteColor.a = fade;
                 sprite.color = spriteColor;
                 yield return null;
             }
             //Make sure it's set to exactly 1f
             fade = 1f;
             spriteColor.a = fade;
             sprite.color = spriteColor;
             yield return new WaitForSeconds(delayToFadeOut);
 
             startTime = Time.time;
             while(fade > 0f)
             {
                 fade = Mathf.Lerp(1f, 0f, (Time.time - startTime) / fadeOutTime);
                 spriteColor.a = fade;
                 sprite.color = spriteColor;
                 yield return null;
             }
             fade = 0f;
             spriteColor.a = fade;
             sprite.color = spriteColor;
             yield return new WaitForSeconds(delayToFadeIn);
         }
     }
 }
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 monopoly10 · Dec 07, 2015 at 06:28 PM 0
Share

Thanks @OncaLupe. This works perfectly!

avatar image monopoly10 · Dec 08, 2015 at 11:57 AM 0
Share

Thank you so much @OncaLupe. This works perfectly!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Filling list with editor scripts is empty on Play 1 Answer

HOW i SOLVE ROOT MOTION PROBLEM WITH ANIMATIONS MADE IN UNITY 0 Answers

How can i auto assign Public value VirtualJoystick to Prefabs 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