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.

You skipped one little thing - in large projects it can take many hours for unity to reimport everything. Moving the project is just like everything else in Unity - broken garbage. In all other engines, you just copy the folder and its done. In Unity you have to waste many hours for this trivial task.

–

1 Answer

1

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.

–

FindComponentsInChildren returns an array of components. If you need to change the color in all of the returned components, try something like this... foreach (Material m in GetComponentsInChildren<Material>()) m.Color = childColor;

–

So I changed the script but there are some issues, I'm going to load the new script into another post.

–