- Home /
change the alpha of a gameobject with multiple childs
Helloo There! I'm trying to change the alpha of a gameobject with multiple child objects, but I would like to know what I am doing wrong. btw the childs have all different mesh renderes. this is what I have:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class FadeHouse : MonoBehaviour { void OnTriggerEnter( Collider other) { if(other.tag == "Player") { List<Renderer>()allChildRenderers; allChildRenderers.AddRange(GetComponentsInChildren<Renderer>()); foreach (Renderer r in allChildRenderers) { Color c = r.material.color; c.a = 0.5f; r.material.color = c; } } } }
oh and I'm getting this error: FadeHouse.cs(13,29): error CS1002: ; expected
Answer by mozhi1997 · Jun 27, 2020 at 03:47 PM
Color is not reference type but value type,so even if you modify var "c" ,but it would not have change with material. https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Color.html You should change like this:
var mat = GetComponent<MeshRenderer>().material;
var originCol = mat.color;
var color = new Color(originCol.r, originCol.g, originCol.b);
color.a = 0.5f;
mat.color = color;
Your answer
![](https://koobas.hobune.stream/wayback/20220613001932im_/https://answers.unity.com/themes/thub/images/avi.jpg)