Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by LMan · May 23, 2015 at 11:24 PM · c#uitextmecanimanimate

How to change color of animated Text component

So I have a text component that is animated to scale up and translate around for a bit. I would like to change it's color through script, but it seems that changing the color on the text component doesn't actually have any effect.

I want to instantiate a prefab and play this animation with a variety of colors. I'd also like to animate the alpha of the color on the text component if that is possible.

I do have write defaults set to false in the animator.

The code that triggers the text to instantiate and play the animation:

 public void TriggerText(string text, Color col)
     {
         Vector3 eulers = new Vector3(0, 180, 0);
         Quaternion rot = Quaternion.Euler (eulers);
         GameObject texteffect = (GameObject)Instantiate (floatingGraphic, Vector3.zero, Quaternion.identity);
         texteffect.transform.SetParent(unitCan.transform); //sets parent to a world space canvas
         texteffect.transform.localScale = Vector3.one;
         texteffect.transform.localPosition = Vector3.zero;
         texteffect.transform.localRotation = rot;
         Animator anim = floatingGraphic.gameObject.GetComponent<Animator> ();
         texteffect.GetComponent<Text>().text = text;
         texteffect.GetComponent<Text> ().color = col;
         Debug.Log (col + "/" + texteffect.GetComponent<Text> ().color);
         anim.Play ("Text Spring in fade out");
     }

The line that calls the method:

 focus_Unit.TriggerText("Turn!", Color.green);


Link to frame by frame video

Comment
Add comment · Show 5
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 barbe63 · May 24, 2015 at 02:20 AM 0
Share

I just tried to add an animator on a text component, animating the color works perfectly fine.

avatar image LMan · May 25, 2015 at 12:46 AM 0
Share

I would like to change it's color through script

I instantiate the prefab, change the color, play the animation. The animator seems to be changing the color back.

The animation doesn't animate the color property at all, though I would love to just animate the alpha, and still set the color through code.

avatar image barbe63 · May 25, 2015 at 03:29 AM 1
Share

Hmm didn't catch that sorry. Well maybe put your code and screenshot then so we could help? I do some color change in one of my text. Here is how it looks like: (C#)

 Color color = timerText.color;
 color.b -= Time.deltaTime * 0.2f;
 color.g -= Time.deltaTime * 0.2f;
 timerText.color = color;

Where timerText is a Text and it's located in an Update but it can be in any kind of loop.

avatar image LMan · May 25, 2015 at 12:51 PM 1
Share

Edited OP to add code and a link to a video. Thanks again for your assistance!

avatar image LMan · May 25, 2015 at 01:00 PM 0
Share

It seems if I add the prefab in edit mode and change the color in the inspector, it works fine. I dunno what is in the code that might change it back only if I instantiate it during runtime.

2 Replies

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

Answer by LMan · May 26, 2015 at 02:26 PM

Deleted the Animator Controller, made a new one, added the same animation clips back in and it worked! The color change through script sticks. Almost definitely a bug, but unfortunately one I can't reproduce.

This bug occurred in Unity 5.0.2f1 Personal, running Windows 7.

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

Answer by barbe63 · May 25, 2015 at 01:19 PM

I think I have an idea of what's going on. It's probably due to your animator who is overriding the color of your text. I don't want to say mistakes but I think remembering I encountered some case where the animator was overriding my color values even if I had no keys with color.

Try first to check if disabling the animator solve the issue, then try to see if a key is related to the renderer it may be the problem. I don't know how you could past that issue if you need the renderer keys or worst if it is indeed due to the animator and no renderer keys are present.

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 LMan · May 25, 2015 at 08:00 PM 1
Share

Hmm at one time the animation did have keys for the color... although I deleted them. I'll try re-doing the animation from scratch and avoid the color property entirely And see if that works. I think you are probably right.

avatar image LMan · May 26, 2015 at 12:09 PM 0
Share

Alright, the animator is definitely the culprit. disabling the animator leaves the color intact. I made a new animation that just translates and scales- no color keys at any time. Still resets the color. I found that changing the culling mode on the animator to anything other than "always animate" leaves the color, but also culls out any translation in the animation.

avatar image LMan · May 26, 2015 at 12:13 PM 0
Share

I bet I could workaround by re-setting the color in late update every frame to counteract the animator, but that's really disappointing if I have to resort to that.

avatar image barbe63 · May 26, 2015 at 12:15 PM 0
Share

I think it deserves a bug report. That shouldn't affect the color if no rendering keys are presents at all.

avatar image LMan · May 26, 2015 at 01:54 PM 0
Share

Tried to reproduce in a clean project... it works. The universe strikes again!

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

20 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

Related Questions

What's your equivalent of old GUIStyle ? 0 Answers

How to measure the width of a string? 0 Answers

How to use special symbols 1 Answer

,InputField backspace returns already send text 0 Answers

Problems to change text from a child in a child 1 Answer


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