• 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 Marsallima · Jan 24, 2012 at 09:49 PM · shadermaterial

How to change material color of an Object

Guys, how may i change the tint color of a material in a existent GameObject in JavaScript?

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

6 Replies

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

Answer by Tasarran · Jan 24, 2012 at 09:54 PM

gameObject.renderer.material.color = Color.white;

You have to either define a color, or you can use Color.white ('white' can be any of a number of colors, including white, red, yellow, black, gray, blue, cyan, green, etc.)

Or build your own Color using Color(R, G, B). R, G, & B in this function are floats from 0.0 to 1.0.

This works best if the base color of the material is 50% gray, that way you have room to move away from that color in any direction.

The actual effect of this is that you are adjusting the 'Main Color' of the material that is exposed in the Inspector at the top of the material.

Comment
Add comment · Show 3 · 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 Marsallima · Jan 24, 2012 at 10:46 PM 0
Share

Thanks :D

avatar image Dumblegore · Jul 01, 2016 at 08:59 PM 3
Share

seems this is deprecated you can update this to:

 gameObject.GetComponent<Renderer> ().material.color = Color.green;
avatar image ajayajayaj · Oct 15, 2016 at 06:31 PM 0
Share

$$anonymous$$A$$anonymous$$E SURE NOT TO USE 0 TO 255! That's what I did.

avatar image
11

Answer by Azbo · Nov 02, 2015 at 01:56 PM

You do not have to do it with code if you want to make it a bit easier. Just left click in your preferred folder in this case Assets and then create-->Material. From there you can change in to a sprite if you want a solid color and you can experiment with them. Once you have the proper color just drag that material into the object.

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 ruileitao · Sep 23, 2013 at 10:02 PM

allo!

When i use

gameObject.renderer.material.color = Color(0.777, 08, 0.604);

the color is not the same as when I use this color in the object.

if anyone knows why, thank you

Comment
Add comment · Show 4 · 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 UnChicoMalo · May 02, 2017 at 06:31 PM -1
Share

Hi! I know this is an old answer but...:

for some reason in code the RGB's 255 = 1, so if you wanted something white (and doing it with the RGB) it'd be:

 gameObject.renderer.material.color = new Color(1,1,1);

So, you can do a simple conversion function:

 private float coloNumberConversion(float num) {
         return (num / 255.0f);
     }

Now you can put the actual RGB you see in the inspector: for example, for a darkish brown

 Color color = Color.black;
 color.r = coloNumberConversion(94);
 color.g = coloNumberConversion(69);
 color.b = coloNumberConversion(31);

avatar image Eno-Khaon UnChicoMalo · May 02, 2017 at 06:47 PM 0
Share

To note, the multiplication in your conversion is redundant. You'll see identical results by using:

 return num / 255.0f;
avatar image UnChicoMalo Eno-Khaon · May 02, 2017 at 08:59 PM 0
Share

Whoops. Corrected, thanks.

avatar image MFaik · Mar 23, 2020 at 01:08 PM 0
Share

I know I am 8 years late but you can use Color32 you give it r g b a from 0 to 255 for example: new Color32(255,0,0,255) is red

avatar image
0

Answer by sixscorpions · Oct 29, 2013 at 06:01 AM

http://solution2tech.blogspot.in/2013/10/change-material-color-in-unity-3d.html

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
0

Answer by CatsPawGames · Sep 15, 2014 at 09:08 PM

If you need more complex recoloring (keeping some colors intact while changing other etc.), you can check Colorify on asset store

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 ruileitao · May 03, 2017 at 01:33 PM 0
Share

Hi guys!

Thank you very much for the support! I have already resolved this issue.

Once again thank you very much!

All the best!

  • 1
  • 2
  • ›

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

19 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

Related Questions

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

Glowing material/shader ? 1 Answer

Find all materials using a specific shader 1 Answer

Standard shader WebGL 0 Answers

Animate changing the material 0 Answers


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