• 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 OctarineSorcerer · Oct 26, 2015 at 12:00 PM · c#uitextcolor

UI text color not assigning correctly

Okay, so after looking at a few things, I thought I'd start scripting some text fading for an intro.

I assign objColour the UI text's colour (using gameObject.GetComponent), and then in the Fade routine, I immediately set it to red (it starts white, the red is for testing purposes). However, the text will not change colour unless the color is directly set from GetComponent.

See this .cs file - the current state of it will not change the text, but if the commented-out line is uncommented, it will change the text. Screenshot

Reason I want to use objColour is because writing gameObject.GetComponent().color = Color(...); every time I wish to set it makes things far less readable.

Unity version is 5.2

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 OncaLupe · Oct 27, 2015 at 05:00 PM 0
Share

Using GetComponent is also bad for performance reasons. Constantly having the game search through the object's components is slow, so it should only be used to store references once, then use that reference later. However, as SlateNeon says in their answer, you're not storing the field reference at the top of your Start() method, but just copying the current color that's there.

Follow SlateNeon's advice to store the Text reference and change the color using that, and you should get what you want.

2 Replies

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

Answer by SlateNeon · Oct 27, 2015 at 12:08 PM

This happens because when you do objColour = GetC().color, you get the color of the text, not the field reference to it. Right now you get a reference to Color object, later you simply change that object to another one, but the text color is still referencing the original color.

To do what you want you should do obj = GetComponent()-text- and then in Fade obj.color = ...

 void Start(){
     obj = GetComponent<Text>();
     ...coroutine...
 }
 IEnumrator Fade(...){
     obj.color = Color.red;
 }

Something like that

Comment
Add comment · Show 1 · 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 OctarineSorcerer · Oct 27, 2015 at 07:11 PM 1
Share

$$anonymous$$any thanks, I see what you mean! I was thinking almost unconditionally that it would return a value by reference, and this clears it up. Works now too, thank you very much.

avatar image
1

Answer by Ashkan_gc · Oct 27, 2015 at 05:23 PM

Color is a struct and in C# structs are passed by value. It means when you get the color of the Text component a copy of the Color struct with the same values of Text.color will be created for you. Then when you change that color, your color will be modified which is a copy and the original Text.color will not be modified. If it was a class (classes are pass by reference) a reference of the Text.color would be provided to you and your modifications would be reflected on Text.color, in order to change the color of the Text you need to store the Text component in a variable and modify its color. var text = gameObject.GetComponent(); text.color=yourDesiredColor;

To make the fade beautiful , other than using Color.Lerp you can use an AnimationCurve and use its Evaluate method to get a value between 0 and 1 for the Lerp based on its curve. then you can play with the curve's shape in editor if AnimationCurve is a public field and visible in inspector. Otherwise you have to change the weight of the Lerp using a formula in code, linear or square or ...

Comment
Add comment · Show 1 · 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 OctarineSorcerer · Oct 27, 2015 at 07:14 PM 0
Share

Thanks for clearing up that structs are passed by value in C# - haven't ever used structs much, so that took me by surprise. Also cheers for the AnimationCurve suggestion, that ought to make things look very nice indeed, and I hadn't thought of it.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change color of animated Text component 2 Answers

Click ui text to dissapear in 3D unity 2017 first person game script for ui multiple texts?,Clicking Text away in Unity 2017 3D first person game 0 Answers

How to update text every time I press space? 1 Answer

How do I change scene with two ui canvas text 2 Answers

Automatically resize scrollview based on vertical text length (Solved) 2 Answers

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