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
0
Question by RSud · Nov 28, 2011 at 01:05 AM · textcolorgui.label

2 instances of text with different colors using OnGUI() and GUI.Label?

I have the following code in OnGUI():

     GUI.color = Color.yellow;
 GUI.Label (new Rect ( 0, 0, 100, 23), "Score: " + gameScore);
     GUI.color = Color.green;            
 GUI.Label (new Rect ( 100, 0, 120, 23), "Host Temp: " + fhostTemp);

But I don't get yellow text and green. If I want two separate colors how do I do this?

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 jahroy · Nov 28, 2011 at 01:39 AM 0
Share

I would expect that to work.

Are you using a GUISkin that overrides the text color for a label?

2 Replies

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

Answer by WillTAtl · Nov 28, 2011 at 02:01 AM

Not sure what the top-level "color" attribute actually does in unity's GUI skins (I could check the docs, but not necessary to answer your question) (Edit: I checked. GUI.color sets a tint that is applied to all gui elements, including images as well as foreground and background colors; since the default gui skin's label text color is black, with the default skin this has no effect at all; if you change the skin's label text color to white, this should work as expected. It would apply that same color to any other gui elements unless reset to clear when you're done, though, so I still think this approach, particularly the second, is the better way to go, personally)

The setting you want to change is GUI.skin.label.normal.textColor - bit of a mouthfull, that's the text color for use in a "normal" state (as opposed to while active, focused, or hovering - none of which seem to apply to labels anyway), on the current skin used by GUI calls.

Example code, two versions:

function OnGUI() { //grab the label style for the current skin var labelStyle=GUI.skin.label;

 //red text  
 labelStyle.normal.textColor=Color.red;
 GUI.Label(Rect(0,0,100,30),"Red",labelStyle);

 //since here I'm using a reference to modify the actual active skin, 
 //you can omit the skin parameter from the Label call, actually, as so...
 //Yellow text   
 labelStyle.normal.textColor=Color.yellow;
 GUI.Label(Rect(0,30,100,30),"Yellow"); //no need to pass it in

 //you could also just modify it directly each time rather than caching a 
 //reference, as so...

 //Blue text 
 GUI.skin.label.normal.textColor=Color.blue; //set it directly on the active 
                                             //skin in GUI
 GUI.Label(Rect(0,60,100,30),"Blue"); //no need to pass it in


 //note that this gui skin is a global object that persists between scenes, 
 //so if another object draws labels after this without explicitly setting
 //the text color, it will use blue labels here. If it's another scene,
 //it might draw blue labels when transitioning from this scene and another
 //color when transitioning from another - not good! So consider the next
 //possibility...

}

That'll do what you're trying to do, but there's another way which is perhaps better than doing this sort of thing entirely in code...

/*alternative approach*/

//I showed you the option of passing the style as a parameter as set up //for this alternative approach...

//three styles for three labels, definable in the inspector var labelStyle1:GUIStyle; var labelStyle2:GUIStyle; var labelStyle3:GUIStyle;

function OnGUI() {

 GUI.Label(Rect(0,0,100,30),"Label 1",labelStyle1);
 GUI.Label(Rect(0,30,100,30),"Label 2",labelStyle2);
 GUI.Label(Rect(0,60,100,30),"Label 3",labelStyle3);

 //this approach avoids the "sticky color" property I described before, 
 //and has the bonus of letting you more easily change any number of style 
 //properties directly in the inspector and see the results faster than you
 //would when changing hard-coded script values.

}

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 RSud · Nov 28, 2011 at 04:44 AM 0
Share

This did it. Thanks!!!

avatar image
0

Answer by VonTalavang · Dec 09, 2011 at 01:53 AM

thanks here too

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotated multi-coloured text 1 Answer

3D Text Object does not change to the color specified on Mouse Over 0 Answers

Color.white not so much white... 3 Answers

changing text color in GUI.Label 3 Answers

render.material.color is not working, or at least not completely? 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