Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
7
Question by Matthew.OC · Jul 06, 2016 at 05:47 PM · c#inputmaterialcolorrgb

Change Color in C# with RGB Values?

Hi everyone, I've been writing a script in C# to change the color of an object when pressing down a key on the keyboard, and that works fine if the code is written like this:

     void Update ()
             {
                         if(Input.GetKeyDown(KeyCode.R))
                         {
                                 gameObject.GetComponent<Renderer>().material.color = Color.red;
                         }
             }

However, instead of changing the color to Red, I want to change it to a color with certain RGB values. (233, 79, 55) Is there a way to do this? I've tried everywhere, but I can't seem to find anything.

Thanks.

EDIT: Ok. So I think I'm getting somewhere. I've tried creating a script:

             void Update ()
             {
                         if(Input.GetKeyDown(KeyCode.R))
                         {
                                 gameObject.GetComponent<Renderer>().material.color = new Color(233, 79, 55);
                         }
             }
 

I think this might work, however when I test the game it changes the material color to white. Does anyone know what I can do to fix it?

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
26

Answer by Cherno · Jul 06, 2016 at 09:59 PM

The problem with your updated script: You pass values above 1.0 to a variable of type Color, which takes values from 0.0 to 1.0, so it ignores anything higher than that and treats it as 1.0. That's why your color is white in the game. The solution is simple: Use Color32 instead, which takes a value from 0 to 255.

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 Bonfire-Boy · Jan 26, 2019 at 12:29 PM 5
Share

Or you can divide by 255.

 new Color(233f/255f, 79f/255f, 55f/255f);

Not saying it's better, just felt that showing how to fix the OP's code with value changes helps to emphasize what they've done wrong.

avatar image
2

Answer by TheBestUnity123 · Oct 19, 2018 at 09:02 AM

@Matthew.OC But in Color32 ,there is a 4th argument which is a or transparency.

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 Eno-Khaon · Oct 19, 2018 at 09:44 AM 0
Share

In a Color, you can use:

 // C# example
 Color c = new Color(0.5f, 0.5f, 0.5f);
 // Same as...
 Color c = new Color(0.5f, 0.5f, 0.5f, 1.0f);


By correlation, the alpha channel would be at the maximum value of 255:

 Color32 c = new Color32(127, 127, 127, 255);
avatar image
1

Answer by bigChris · Jan 25, 2019 at 07:56 PM

Lets say you've come up with the perfect shade of grey (or gray). In the color picker, you note that "Perfect Grey" is "ADADAD" and the Alpha is 200. In your game, the myGameObject's color gets changed to Red for whatever reason, but later, your GameObject is newly invigorated and its color must change back to "Perfect grey". The call to do this is:

 myGameObject.color = new Color32( 173, 173, 173, 200 );

Where did the 173 values come from? The "AD" in the color picker is a hex value. Convert that to a decimal and you have "173". If you didn't get a BS in Computer Science and/or can't convert that in your head, there are any number of Hex to Decimal converters on line. The Alpha (opacity) value "200" is conveniently displayed in the color picker as a decimal.

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
1

Answer by Rugbug_Redfern · Jan 27, 2019 at 06:20 PM

The Color class uses values from 0 to 1, not 0 to 255. So your color should be

  new Color(0.91, 0.3, 0.21);

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

11 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Gui list and color 0 Answers

Why are there color differences between photoshop and Unity? 5 Answers

Simple question about materials 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