- Home /
Can't get children/Color not part of material
So I have a 3d game with a player that consists of an empty object with three children, one black engine and two body parts that need to change color.
using UnityEngine;
using System.Collections;
public class Collection : MonoBehaviour
{
public Transform tForm;
public GameObject gObject;
private bool isGreen = false;
private bool isRed = false;
void OnTriggerEnter(Collider other)
{
//green
if (other.gameObject.tag == "GreenCube")
{
other.gameObject.SetActive(false);
isGreen = true;
}
//red
if (other.gameObject.tag == "RedCube")
{
other.gameObject.SetActive(false);
isRed = true;
}
}
private Color childColorGreen;
private Color childColorRed;
public void ChangeColor ()
{
childColorGreen = new Color(0, 0, 1, 1);
childColorRed = new Color(1, 0, 0, 1);
if (isGreen == true)
{
gObject.FindComponentsInChildren<Material.Color>() = childColorGreen;
}
if (isRed == true)
{
gObject.FindComponentsInChildren<Material.Color>() = childColorRed;
}
}
}
Also, how do I get specific child? I need two child from "Player;" I need "Body" and "Front."
Answer by Larry-Dietz · Nov 30, 2019 at 05:53 PM
Try something like this....
transform.Find("Body").gameObject.GetComponent<Material>().Color = childColorGreen;
Same idea for Front.
-Larry
Your script seems like it should work but I get this error:
Assets\Scripts\Player\Collection.cs(42,72): error CS1061: '$$anonymous$$aterial' does not contain a definition for 'Color' and no accessible extension method 'Color' accepting a first argument of type '$$anonymous$$aterial' could be found (are you missing a using directive or an assembly reference?)
Thats what I get for writing off the top of my head :)
Change Color to color (lower case c) and try it again.
Thanks. I got no more errors but for some reason, they won't switch color. I think I don't have the right material for them or something.