• 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
Question by milanow · Aug 02, 2015 at 11:48 AM · shadertexturematerialcolor

How to access Emission Color of a Material in Script?

Hi, so I need a way to change the color of an Object over texture. So I guess the emission light color should be what I want. But I simply call it in Unity by using Material.SetColor("_Emission", newColor) as described in UnityDoc:

http://docs.unity3d.com/ScriptReference/Material.SetColor.html

But it seems not working. Am I right to use dat function? BTW, it could be changed in Inspector.

Comment
Xonatron
cobertos
BurmasGG
miha4406jp
Unshackled

People who like this

5 Show 0
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

5 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Unshackled · Dec 10, 2017 at 12:38 AM

I changed the emission color with t$$anonymous$$s code:

 Material mymat = GetComponent<Renderer>().material;
         mymat.SetColor("_EmissionColor", Color.red);
Comment
Puebebe
RodrigoSeVeN
errorcache
austinborden
cobertos
RizwanMustafa
BurmasGG
quangtqag
goodos_
dynid
deivid-01
unity_bTkJGG31azdikQ
Sneirox
ALIENPANDA
marcfinger123
And 5 more...

People who like this

20 Show 0 · 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

Answer by magique · Oct 28, 2015 at 12:52 PM

@millanow, You need to call the following before setting the _EmissionColor.

gameObject.GetComponent).material.EnableKeyword("_EMISSION");

I had the same issue and found t$$anonymous$$s solution elsewhere.

Comment
Daiyunchao
Zmeyk
RodrigoSeVeN
BurmasGG
quangtqag
moritzcerst
GatesFXP
Sneirox

People who like this

8 Show 0 · 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

Answer by terresquall · Feb 10, 2020 at 05:18 AM

You have to make sure that the Emission property on the material is enabled, w$$anonymous$$ch you can do using: material.EnableKeyword("_EMISSION");. You will also have to use DynamicGI.UpdateEnvironment(); if you want the emission lighting to affect the environment. Take a look at t$$anonymous$$s article. It explains all the little details you have to take care of to get emission lighting to work properly: https://blog.terresquall.com/2020/01/getting-your-emission-maps-to-work-in-unity/

Comment
mhmtbtn
GatesFXP
M_R_M
Sneirox

People who like this

4 Show 0 · 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

Answer by CHPedersen · Aug 02, 2015 at 11:49 AM

Calling Material.SetColor("_Emission", newColor) only has an effect if the shader w$$anonymous$$ch is used to render the object actually has a variable called _Emission defined in it. T$$anonymous$$s is the case for Vertex Lit shaders, as the docs also describe. Are you using a vertex lit shader?

Comment

People who like this

0 Show 8 · 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 milanow · Aug 02, 2015 at 05:31 PM 0
Share

No, actually I only use default Standard shader. Does it have a variable defining emission light?

avatar image CHPedersen · Aug 02, 2015 at 05:35 PM 0
Share

Nope. :) Select the vertex lit shader like the docs say, and you'll see the Emissive color in the inspector:

Emissive color

No emissive color in the shader = no reaction when setting the color using SetColor.

emission.png (14.0 kB)
avatar image milanow · Aug 03, 2015 at 04:05 AM 0
Share

@CHPedersen it works but not the way I want...So if I have a texture contains black, I actually want a red color could be kinds up covering that black. In VertexLit shader, the red is still covered by black. If I use standard shader and change Emission in the inspector, the Emission color could do that.

So I guess if I could check standard shader file and figure out which variable represents "Emission" in the inspector. I could still use standard shader. But obviously there is no ways to check out source code in Unity.......

avatar image CHPedersen · Aug 03, 2015 at 12:25 PM 0
Share

Sure there is. :) Just not from within the editor itself. You can view the source code for the built-in shaders if you download them from this site. Click the link under "Additional downloads" and select built in shaders.

After you get them, you'll have to study the standard diffuse shader and standard vertexlit shader a bit, of course, to figure out how to add the emissive functionality to the diffuse one. It won't be terribly difficult, but you have to be at least comfortable with shader code to do so.

avatar image milanow · Aug 04, 2015 at 07:47 AM 0
Share

@CHPederson Haha you know what? Standard shader does have a definition for Emission called "_EmissionColor". But the weird thing is when I run it in the Editor, emission color was still black even though I called SetColor("_EmissionColor". Color.red) func. as below,

alt text

However when I unfolded the material window (game is on), it change to red immediately....OMG alt text

Have you ever met this before?

capture1.png (5.8 kB)
capture2.png (20.5 kB)
avatar image moritzcerst milanow · Dec 18, 2020 at 10:56 PM 0
Share

I had the same bug. When i changed the Global Illumination, everything worked perfectly.

Show more comments
avatar image

Answer by agenda · Aug 13, 2017 at 12:40 PM

t$$anonymous$$s is because the shader wasnt in the scene at the start containing a entry called emission! if u apply and enable emission through script without having a copy with those features in the scene from start it fails!

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Materials are colored differently on rotated objects 1 Answer

Texture Takes Main Color of Material 1 Answer

Double Layer Material 0 Answers

Changing Eye Colour (Colour only non-white parts of a texture?) 2 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