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
0
Question by $$anonymous$$ · Jul 10, 2017 at 01:55 PM · graphicscolormaterialscolor changeemission

Why does changing this object's color using Material.color work at all? (C#)

There are a couple places in my project where I need something to change color. I'm not using any light sources; instead, I've been setting the emission color in the material settings. In my game, the player has a flamethrower, which overheats when you use it for too long (so you have to let it cool down). Recently, I wanted the color of my player to change from black to red, in order to indicate that the flamethrower was overheating. I first tried with this code, in Update():

 bodyMaterial.color = Color.Lerp(blackColor, redColor, firingTimePassed / flamethrowerOverheatTime);

This didn't work, because using the Material.color variable was the 'same as using GetColor or SetColor with "_Color" name' (Unity Script Reference). Without any light sources, the color seems to stay the emission color (black). So, I updated this code to:

 bodyMaterial.SetColor("_EmissionColor", Color.Lerp(blackColor, redColor, firingTimePassed / flamethrowerOverheatTime));

Which works perfectly. But... this led me to remember another piece of code I wrote much earlier, for the fire particles themselves. I wanted the fire particles to appear with slight variations in color, so in the Start() function I wrote:

 material = gameObject.GetComponentInChildren<Renderer>().material;
 float randomRednessModifier = Random.Range(-colourOffsetRange, colourOffsetRange);
 Color color = material.color;
 color.r += randomRednessModifier;
 material.color = color;

And it works. But why? How? There aren't any light sources, so I would've thought I'd need to change the _EmissionColor property rather than the _Color property? Just so you know, the player is completely black by default, and the fire particles are completely red.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Jul 10, 2017 at 08:04 PM

the simplest way to change the main color of the material shader is like this:

 transform.renderer.material.color=Color.red;

however that just changes the main color. if your shader has multiple color options you can change the other colors like this:

 transform.renderer.material.SetColor("_SpecColor", Color.red);

if your shader has slider bars or adjustable floats you can change them like this:

transform.renderer.material.SetFloat("_Shininess",0.5f);

anyways part of your problem is that if your character's actual texture is black. it will always be black. When the shader's color propery tints a color. It just muliplys the two colors for the output. so colors can be deepend and darkened but not lightened. in other words you can't lighten a black texture to be red. the tint color does nothing to black. (since the RGB of black 0,0,0,0 * whatever tint color will always be zero).

you could make your original texture white. and use the color property to make it black. then you would be able to switch to red.

other than that you could change some reflective color (with my second code example above) and hope light reflection makes him look more redish.

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

75 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 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 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 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

My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers

How to change material color to a specific object group's childs..? 1 Answer

Array of colors not working, all sprites turn white. 1 Answer

Changing Splash Screen background Color with script?HELLLLP 1 Answer

Apply gradient between two colors on x amount of objects 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