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 $$anonymous$$ · Mar 28, 2018 at 09:56 AM · time

Issues with fading text.

Hi, this is my first time posting here but I was wondering if someone would be able to help me with an issue I'm having with my code. Whenever I run this code, which is meant to slowly fade text by reducing its alpha, the alpha instantly sets to zero. I'm not sure if this is because the fade is happening to fast or there is an error in my code. Here is my code:

 public void Fadeout (int Player, string sign, int value, int position) {
         PlayerOneSavingsModIndicator.color = new Color (PlayerOneSavingsModIndicator.color.r, PlayerOneSavingsModIndicator.color.g, PlayerOneSavingsModIndicator.color.b, 1f);
         if (Player == 0) {
             if (position == 1) {
                 PlayerOneSavingsModIndicator.text = sign + value;
                 if (sign == "-") {
                     PlayerOneSavingsModIndicator.color = Color.red;
                 } else if (sign == "+") {
                     PlayerOneSavingsModIndicator.color = Color.green;
                 }
 
                 while (PlayerOneSavingsModIndicator.color.a > 0.0f) {
                     PlayerOneSavingsModIndicator.color = new Color (PlayerOneSavingsModIndicator.color.r, PlayerOneSavingsModIndicator.color.g, PlayerOneSavingsModIndicator.color.b, PlayerOneSavingsModIndicator.color.a - (Time.deltaTime / 10));
                 }
 
             } else if (position == 2) {
             }
         } else if (Player == 1) {
         } else if (Player == 2) {
         } else if (Player == 3) {
         }


Any help would be greatly appreciated. Thank you.

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 Legend_Bacon · Mar 28, 2018 at 09:58 AM

Hello there,

There are easier ways to fade Text elements in an out, like myText.CrossFadeAlpha().

But if you want to have more control over the process, you can use something like this:

 ///target = the Text component we want to fade
     ///duration - How long the fade should take
     ///targetAlpha - What should the alpha be at the end
     ///[Optional] unscaledTime - Set to true if you want the fade to happen independently of timeScale
     private IEnumerator FadeText(UnityEngine.UI.Text target, float duration, float targetAlpha, bool unscaledTime = false)
     {
         float timer = 0.0f;
         float startAlpha = target.color.a;
 
         // Start Fade
         while (timer < duration)
         {
             // 
             timer += unscaledTime ? Time.unscaledDeltaTime : Time.deltaTime;
             float t = timer / duration;
             t = t * t * t * (t * (6f * t - 15f) + 10f);
             float curTargetAlpha = Mathf.Lerp(startAlpha, targetAlpha, t);
             Color targetColor = new Color(target.color.r, target.color.g, target.color.b, curTargetAlpha);
             target.color = targetColor;
 
             yield return null;
         }
 
         // Fade has finished
         yield return null;
     }


You can call is with StartCoroutine(FadeText(myText, 2.0f, 0.0f));.


Hope that helps!

Cheers,

~LegendBacon

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

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

Camera following Bullet 1 Answer

reloadtime 1 Answer

Collision Detection Issues 0 Answers

Lots of !IsFinite() Errors and FPS crawling to a halt - How to debug? 1 Answer

How to change value of variable over period of time? 2 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