- Home /
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.
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?
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.
$$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.
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?
"_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.
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.