- Home /
FindChild/Change color
So I'm making a script to change the color of two parts of my player ( a 3 piece rocket ship). The body and front need to change color while the engine stays black. It needs to change the colors when it enters an object but I'm having problems calling the child and I'm not sure if they will change color.
public class Collection : MonoBehaviour { public Color White; public Color Green; public Color Red;
public Transform tForm;
public GameObject gObject;
private gObject.tForm.Find.("Body") = body;
void OnTriggerEnter(Collider other)
{
//green
if (other.gameObject.tag == "GreenCube")
{
other.gameObject.SetActive(false);
//gObject.tForm.Find("Body");
//Body.material.SetColor("_Color", Green);
}
//red
if (other.gameObject.tag == "RedCube")
{
other.gameObject.SetActive(false);
}
}
}
I've only started on the green, I'll do the Red once the answer is given.
Answer by PruneDealer · Nov 29, 2019 at 09:55 PM
Well if the game object in this script is the body and the other parts are child objects you should be able to do something along the lines of this
private Color childColor;
public void ChangeColor()
{
childColor = new Color(1, 0, 0, 1) //red
gameobject.FindCompenentsInChildren<Material.Color>() = childColor;
}
I might be totally wrong on this but that's my best guess. Good luck @Jacobsc
Thanks! I've been trying to use public but I guess doing ChangeColor would be better. But I get errors with the current child calling function I have.
Be sure to check against your parent object, as findcomponentsinchildren also returns the object the script is running on
FindComponentsInChildren returns an array of components. If you need to change the color in all of the returned components, try something like this...
foreach ($$anonymous$$aterial m in GetComponentsInChildren<$$anonymous$$aterial>())
m.Color = childColor;
So I changed the script but there are some issues, I'm going to load the new script into another post.