Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ragefordragons · Nov 08, 2018 at 03:44 AM · uiprefabtextcolor

UI Text.color not assigning?

So I made method to remotely spawn damage numbers, and I wanted to make it so you could change the color as well. However, the color never seems to change from gray. Whats weird is that when I changed the prefab's text color, it was set to gray still when I ran the game, so I am quite confused. Any idea why the color isn't being set?

{

 public Animator anim;
 private Text damageText;


 void Start () {
     AnimatorClipInfo[] clipInfo = anim.GetCurrentAnimatorClipInfo(0);
     Destroy(gameObject, clipInfo[0].clip.length - 0.1f);
     damageText = anim.GetComponent<Text>();
 }
 
 public void SetText(string text)
 {
     anim.GetComponent<Text>().text = text;
 }

 public void SetColor(string color)
 {
     damageText = anim.GetComponent<Text>();


     if (color == "Gray")
     {
         damageText.color = new Color(0.5f, 0.5f, 0.5f, 1f);
     }
     else if (color == "Red")
     {
         damageText.color = Color.red;

     }
     else
     {
         Debug.LogError(color + " is not a valid color!");
     }
 }

}

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 AaronXRDev · Nov 08, 2018 at 05:44 AM 0
Share

First thing I would check is if the value you are passing to SetColor(string color) matches the case that the script is expecting (e.g. "red" ins$$anonymous$$d of "Red"). If this is working correctly the next thing I would check is that the animator component is on the same object as the text component.

Are you getting errors or warnings in the console when it runs?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by s_awali · Nov 08, 2018 at 02:00 PM

I see you have an animator on the GameObject. If your animator has a single animation that modify the color of your Text, even on 1 single frame, then this property is "locked", and will always be overriden by the animation, no matter what you do in code.

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 ragefordragons · Nov 08, 2018 at 03:04 PM 0
Share

Oh! Yes that would explain why. It always plays an animation that makes it fade out when it gets instantiated. I wonder what the work around for this would be? Could I maybe make two animation clips?

avatar image s_awali ragefordragons · Nov 08, 2018 at 04:05 PM 0
Share

Nope, if 1 animation set the color, even if the animation is not playing the value is locked. You should ins$$anonymous$$d add a CanvasGroup component to the parent of your button, and change the alpha value. This will fade every child.

avatar image
0

Answer by swanijam · Nov 08, 2018 at 04:31 PM

I'd recommend doing the animated alpha fade in a coroutine, on a script. This way you can apply it to a specific Text without creating extra canvas elements, you don't have to animate it with keyframes, and you can still very easily change the curve it uses to interpolate. The coroutine stops after the duration and releases control of the alpha value forever. Here's the code I use frequently to do this:

 public Text text;
     float fadeDuration;
     Color maximumOpacityColor, minimumOpacityColor;
     public AnimationCurve animationCurve;
     private void Start()
     {
         StartCoroutine(InterpolateColor(maximumOpacityColor, minimumOpacityColor, fadeDuration));    
     }
     
     private IEnumerator InterpolateColor(Color minColor, Color maxColor, float _time) {
         float currTime = 0f;
         while (currTime < _time) {
             currTime += Time.deltaTime;
             float lerpVal = Mathf.InverseLerp(0f, _time, currTime);
             text.color = Color.Lerp(minColor, maxColor, animationCurve.Evaluate(lerpVal));
             yield return new WaitForEndOfFrame();
         }
     }
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

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

How To Change Color Of Text On UI When It's Selected | Unity 4.6 3 Answers

Text UI field input doesn't get saved in my prefab 0 Answers

Trouble instantiating ui text 2 Answers

Modify individual letters in new Unity UI Text 1 Answer

UI text color not assigning correctly 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