• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

180 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 avatar image

Related Questions

Multiple Rich Text Not Working 1 Answer

Gradient Text in Unity 5.2.2 "BaseVertexEffect is obsolete use BaseMeshEffect instead" 3 Answers

UIMask and UIText problem 0 Answers

change button and text color together 1 Answer

Trouble instantiating ui text 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges