- Home /
Changing alpha of a child Component in c sharp?
Hello,
I have an object called SwitchColorChanger which has a child plane component called PlaneLight. PlaneLight has a Particles/Alpha Blended Material which has a texture map on it. I want to either fade the rgb values up & down over time or fade the aplha value up & down over time to get a slowly pulsing glow effect. This will be pulsing for the lifetime of the object so most likely I need to hold some kind of reference for efficiency's sake. I'm having difficulty with this. I think I need to use GetComponentInChildren but I'm not sure how. Any help would be appreciated.
EDIT - for some reason clicking on comment isn't working at the moment so I'm posting my response to Jessy's reply here, with apologies for the unorthodox way it's been done:
'Thank you Jessy! That's much appreciated.
In reference to your query (for future people which come across this) The parent object is called SwitchColorChanger and has a script of the same name attached where this code goes. It has a child game object called PlaneLight which has a plane, a mesh renderer and a material called m_ColorChangerlight, which is a shader of type Particles/AlphaBlended. It was that material that I wanted to affect over time.
Now all I have to do is figure the most efficient way to increase & decrease the Alpha property over time and I'm sorted. And grateful. Thanks again Jessy.'
Answer by Jessy · Jan 12, 2011 at 03:47 PM
The most efficient reference you can cache is to the material itself. I don't know if you're saying that the Game Object is called PlaneLight, that a child has a script called PlaneLight attached, or both. I'm going to go with the previous assumption here, and you tell us if that's incorrect(*).
Material material;
void Awake () { material = transform.Find("PlaneLight").renderer.sharedMaterial; }
float Alpha { set { Color color = material.GetColor("_TintColor"); color.a = value; material.SetColor("_TintColor", color); } }
(*) Here's what you might do if PlaneLight is a class:
material = GetComponentInChildren<PlaneLight>().renderer.sharedMaterial;
Answer by jonc113 · Sep 03, 2012 at 12:44 AM
I found this answer by looking at the code of iTween, as it can control the alpha of children.
The trick is:
foreach(Transform child in PARENTGAMEOBJECT)
{ DO_WHAT_YOU_WANT_WITH child.gameObject) ; }
This allows you to get the gameObject of all children - then you can deal with them the same way as any game object. My problem was in using `GetComponentsInChildren()` which returns components - and you can't change the color of components.
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
change tint of image 0 Answers
Text not reverting back to original properties after gameplay 1 Answer
Noise on transparent material. 0 Answers
Way to get a material to alpha-fade along the texture? 0 Answers