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
1
Question by RyanZimmerman87 · Feb 22, 2014 at 10:03 AM · materialcoloroafa

Why can I change materials but not material color???

Alright so I got this scene set up.

I have a stealth enemy named "Mad Dog". If the player has a certain "Baby Blue Beast" pet active then Mad Dog is supposed to be visible because the Baby Blue Beast can detect stealth.

I just wasted about 2 hours trying to manually change the color values for the "Mad Dog Stealth" material but I can't get it to work.

But then when I make 3 total materials for all the possible Mad Dog material states (Normal, Stealth with pet, Stealth without pet), it works perfectly fine.

Why can I not get my one stealth material to change the alpha transparency value in real time? Why do I have to make an additional material to account for a semi visible and completely invisible material state for "Mad Dog".

I am SOOOO sick of running into these problems and wasting hours of time in Unity doing something that according to their neanderthal like documentation should NEVER be even slightly difficult to accomplish.

So why can I not adjust the values of the material colors during runtime after spending like 2 hours. But then as soon as I give up and just make a new material for the color change I can get it to work in 30 seconds????

It's a very simple problem. I set up a script on the parent object which contains a public renderer variable. The renderer variable can switch between the normal visible material and the invisible material (both public material variables on the parent object).

Then in the Script it's supposed to be able to determine first if the players in range, switch to stealth material. And then whether the player has the right pet active to change the material color in real time.

No matter what I tried I could not get the material color to change with the pet swaps. However simply making a new material to use instead of changing the materials alpha transparency with code worked instantaneously.

WHY DO THE CODE ASPECT OF UNITY NOT WORK??? It's like the game engine is truly broken and many features don't work you have to just create more variables to substitute for features that should work with simple code according to the documentation...

Can anyone tell me how to get this to change color with code instead of using a different material????

 void Update()
 {
             
 //mad dog in range of player        
 
 if (inRangeofPlayerBigTrigger == true)
    { 
 //if pet active use the alpha = 25 transparency
             
 if (PlayerHealthBarScript.petActiveBool == true)
 {
    enemyRenderer.material = stealthMaterial;
 }
 
                       
 //if pet is not active use the alpha = 0 transparency
 
 else if (PlayerHealthBarScript.petActiveBool == false)
 {
   enemyRenderer.material = stealthMaterialFull;
 }
             
 }


After spending 2-3 hours plus on this simple problem that is the fruits of my labor. I have to use a separate material directly because all attempts to manually edit the color at run time were not successful.

It's not even worth it to post code examples of how the syntax is supposed to be to change colors, I already did it, it makes 100% logical sense and causes no compilation errors yet it doesn't work.

Someone give me an example how you can replace the "stealthMaterial" and "stealthMaterialFull" with just changing the color values of the stealth material...

It's really a pain to still not be able to do the simplest of tasks in Unity. Whenever I think I finally have a grasp on Unity I try something new like this that fails horribly.

Comment
Add comment · Show 3
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 whydoidoit · Feb 22, 2014 at 10:44 AM 0
Share

I sympathise with how you are feeling - but systems as complex as Unity have lots of gotchas that hide in the cracks.

What shader are you using to for the material that didn't work and how was that code affecting the material colour?

avatar image RyanZimmerman87 · Feb 23, 2014 at 02:10 AM 0
Share

So my whole set up is like this:

One public Renderer variable on the parent object on the main enemy script. I set this variable to the "Body" child object which is a skinned $$anonymous$$esh Renderer.

I also have 3 public material variables. So those variables are like this on the script:

 public Renderer enemyRenderer;
 
 public $$anonymous$$aterial normal$$anonymous$$aterial;
 public $$anonymous$$aterial s$$anonymous$$lth$$anonymous$$aterial;
 public $$anonymous$$aterial s$$anonymous$$lth$$anonymous$$aterialFull;


This is how it's supposed to work:

normal$$anonymous$$aterial is the default material just a simple $$anonymous$$obile/Diffuse shader.

Both s$$anonymous$$lth$$anonymous$$aterial and s$$anonymous$$lth$$anonymous$$aterialFull are identical textures I am just switching the shader on those materials to Particles/Additive.

I want it to work so that when this enemy is close to the player he switches from normal material to the s$$anonymous$$lth$$anonymous$$aterial. This works fine.

But then I want to be able to control the alpha value of the s$$anonymous$$lth$$anonymous$$aterial with code. I haven't been able to figure out a way to do this so ins$$anonymous$$d I'm using 3 materials for all 3 possible rendering states of this enemy.

I want to know how to adjust the s$$anonymous$$lth $$anonymous$$aterial Color for the Particles/Additive Shader "s$$anonymous$$lth$$anonymous$$aterial". So far I cannot figure out how to do this with code even though it seems like it should be very easy.

I've tried a bunch of different things already but this is a basic code example of how I would expect it to work.

 void Update()
 {
 
 //if in range of player switches to s$$anonymous$$lth material
 if (inRangeofPlayerBigTrigger == true)
 { 
 
 //if player has pet active s$$anonymous$$lth material alpha color value should be 25
 //this makes the enemy slightly visible
 if(PlayerHealthBarScript.petActiveBool == true)
 {
 //this works to switch material
 enemyRenderer.material = s$$anonymous$$lth$$anonymous$$aterial;
 
 //this does not work to change color
 enemyRenderer.material.color = new Color (128, 128, 128, 30);
 }
 
 //if player does not have pet active s$$anonymous$$lth material alpha color value should be 0
 //this makes the enemy invisible
 else if(PlayerHealthBarScript.petActiveBool == false)
 {
 //this works to switch material
 enemyRenderer.material = s$$anonymous$$lth$$anonymous$$aterial;
 
 //this does not work to change color
 enemyRenderer.material.color = new Color (128, 128, 128, 0);
 }
 
 }

That's a pretty sloppy example of how I'm trying to implement this with it being in Update() like that but at this point I just want to get it working for starters.

I also have tried setting the material directly like:

 if (PlayerHealthBarScript.petActiveBool == true)
 {
 s$$anonymous$$lth$$anonymous$$aterial.color = new Color (128, 128, 128, 30);
     
 enemyRenderer.material = s$$anonymous$$lth$$anonymous$$aterial;
 }

That does not work either...

For this particular example I could just use all 3 materials and switch accordingly. But I really want to know how to change the colors for this kind of thing with code because there are many other useful applications.

And I have the material set to an Alpha value of 25, but in my code I'm using 30 to see if it will change it from being 25 to 30 but it doesn't.

avatar image RyanZimmerman87 · Feb 23, 2014 at 02:43 AM 0
Share

$$anonymous$$ I even added a public color variable to make sure that's working and it does. However applying this correct color to the material is not having any effect.

So it seems the entire issue is I cannot even make the enemy renderer material change color to begin with.

1 Reply

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

Answer by RyanZimmerman87 · Feb 23, 2014 at 04:27 AM

K I FINALLY figured it out what a joke Unity is with this kind of thing, terrible documentation!

They don't have one single mention of "_TintColor" that I can find in the official documentation.

So basically I waste hours trying to figure out how to do something that's very unclear due to bad documentation.

So the correct way to do this is something like:

 //for semi visible stealth
 enemyRenderer.material = stealthMaterial;
 
 //this is equivalent to RGBA of : (128, 128, 128, 25)
 stealthColor = new Color (0.502f, 0.502f, 0.502f, 0.098f);
 
 stealthMaterial.SetColor ("_TintColor", stealthColor);
 
 
 //for invisible stealth
 enemyRenderer.material = stealthMaterial;
 
 //this is equivalent to RGBA of : (128, 128, 128, 0)
 stealthColor = new Color (0.5f, 0.5f, 0.5f, 0f);
 
 stealthMaterial.SetColor ("_TintColor", stealthColor);


So it's nearly impossible to know about "_TintColor" from the documentation.

And I also find it odd how in the Unity Editor the "Tint Color" is set with RGBA but with code you need to use a 0-1 value. Makes you have to pretty much debug your own colors to even find the right values, a bit counterproductive and excessively tedious in my opinion.

Also not really sure why (128, 128, 128, 25) gives different RGB values than (128, 128, 128, 0) according to the 0-1 float values. One is .502f the other is .5f?

Comment
Add comment · Show 2 · 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 CaptainFoxx · May 04, 2018 at 11:36 AM 2
Share

"_TintColor" won't be documented because it is a shader variable that is used somewhere in the render calculation. Lots of shaders have a "_Color" value as default. This allows the default color setting and getting to work. It is worth checking within the shader that your material is using to find out what variables it is using.

avatar image LaurentC · Jun 11, 2020 at 07:07 AM 0
Share

CaptainFoxx. I agree that shaders can have their own variable names. But anyhow, Unity should provide some examples on how to get to know those variables and call them in script. Also, it should be made that all unity shaders, have a common ground in terms of na$$anonymous$$g variables. And that a "best practice" notice should exist for people creating shaders and puting them on the Asset store, so that na$$anonymous$$g remain consistent for everyone to enjoy and play with.

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

22 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

Related Questions

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

Changing two different objects renderer colour 1 Answer

sharedMaterial.color not saving through scenes on build 0 Answers

Change water material Refraction color? 1 Answer

Standard Shader Decals? 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