Changing alpha channel of the tint color on a material
I see the same question all over but they're all talking about sky boxes and shaders.
I have a shield bubble around my ship, and I want it to disappear visually, and reappear when something hits it. I wrote this script here.
public Color myColor;
void Update () {
if (myColor.a > 0)
{
myColor.a -= 0.1f;
}
GetComponent<Renderer>().material.color = myColor;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Asteroid")
{
myColor.a += 150;
}
}
On runtime, it tells me that the material has no color property. The material is using the particles/additive shader, which has the tint color property. How would I access that property?
When I try the script on the standard shader, it doesn't even work they way I think it should. When an asteroid hits it, it puts it at 255 alpha instead of 150, and it doesn't deplete unless I open the color picker tool, which forces it to update I guess. I tried making the color variable private.
I still don't know how to access tint color, but in case someone else stumbles upon this question looking for answers, I did solve the latter half of my problem.
It seems like when I access myColor.a, it isn't on an intuitive scale of 0-255, but ins$$anonymous$$d 0.0f - 1.0f.
also keep in $$anonymous$$d that when you change alpha by script that it is a float from 0 to 1. also remember that you need to have a transparent shader on the object for this to work!
Answer by saschandroid · Apr 07, 2016 at 07:48 AM
Have you tried to manipulate the tint color of the shader directly?
GetComponent<Renderer>().material.SetColor("_TintColor", myColor);
PS: If you have selected your material, you can click on the little gear icon in the inspector and then on Edit Shader. Now you can see which properties the shader has and what their string representation is to access them.
YES, THAN$$anonymous$$YOU. That's exactly what I was asking for, I didn't know how to do that. And it's a good thing to know about the shader strings. I was settling for a standard transparent shader ins$$anonymous$$d of the additive, but now I don't have to. Thanks again.
I have converted my comment to an answer, please mark it as the correct one.
I wish I could do that, as the thread op. This isn't the first time I've been answered in comment form.
Answer by JigneshKoradiya · Apr 07, 2016 at 05:42 AM
public Color myColor;
void Update () {
if (myColor.a > 0)
{
myColor.a -= 0.001f;
}
GetComponent<Renderer>().material.color = myColor;
} void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Asteroid")
{
myColor.a += 1;
}
}
// use this script and please check if material of your object must have color property
I had tried messing with the values, and I figured that part out. I was asking how to access the tint color property on a shader that didn't have a regular color property. saschandroid gave me what I was looking for. GetComponent().material.SetColor("_TintColor", myColor);
paste your shader code here if u are using any shader or give your shader name if u are using unity shader
Answer by jhuynh_isobar · Sep 02, 2016 at 07:48 PM
I'm using Unity's: Particles/Additive shader, and I tried this code, but i keep getting an error: Material doesn't have a color property 'Color' UnityEngine.Material:get_color() FadeCylinder:MaxAlpha() (at Assets/Wyndham/Sandbox/CocktailTransition/Script/FadeCylinder.cs:47) c_Iterator1C:MoveNext() (at Assets/Wyndham/Sandbox/CocktailTransition/Script/FadeCylinder.cs:81) UnityEngine.MonoBehaviour:StartCoroutine(String, Object) FadeCylinder:FadeIn(Single) (at Assets/Wyndham/Sandbox/CocktailTransition/Script/FadeCylinder.cs:136) FadeCylinder:FadeIn() (at Assets/Wyndham/Sandbox/CocktailTransition/Script/FadeCylinder.cs:125) FadeCylinder:Update() (at Assets/Wyndham/Sandbox/CocktailTransition/Script/FadeCylinder.cs:153)
Your answer
Follow this Question
Related Questions
Using Texture as HDR color? 2 Answers
Tint Multiple Textures Separately 1 Answer
Modifying shader requires restarting application 0 Answers
Tree Creator textures seem missing 1 Answer
Tint a UI sprite with a gradient 0 Answers