Changing color of gameobjects within a gameobject

2631627--184992--sofa.png

So I have this sofa that has multiple gameobjects within it, notably cushions on top of it. 2631627--184993--SofaForGame.png
2631627--184994--sofarenderer.png

What I intend to do is change the color of all parts of the sofa. I’ve already done so for things like chairs but those are single objects and this sofa has multiple. I’ve tried some methods involving arrays but so far I can only change the color of one cushion.

Here’s the code that I’m using, thanks for any help.

 private GameObject furniture;
    public GameObject ColorMenu;
    private bool canvasisopen;
    int layermask = 1 << 9;
    public Texture[] textures;
    public Renderer[] rend;
    private Material[] materials;
    private Color[] colors;
    // Use this for initialization
    void Awake()
    {
        canvasisopen = false;
        layermask = ~layermask;

    }
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetMouseButtonDown(2))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Physics.Raycast(ray, out hit,Mathf.Infinity,layermask);
            furniture = hit.transform.gameObject;
            rend = furniture.GetComponentsInChildren<Renderer>();


            if (furniture.tag == "GroundFurniture" || furniture.tag == "WallArea" && canvasisopen == false)
            {

                Debug.Log(furniture.name);
                ColorMenu.SetActive(true);
                canvasisopen = true;
            }
            else
            {
                canvasisopen = false;
                ColorMenu.SetActive(false);
            }
        }
    }



    public void ChangeToRed()
    {
       
        for (int i = 0; i < rend.Length; i++)
        {
            materials = rend[i].materials;
            //Debug.Log(rend[i].name);
            Debug.Log(rend[i].material.name);
        }

        for (int z = 0; z < materials.Length; z++)
        {
            materials[z].mainTexture = null;
            materials[z].color = Color.red;
        }
       
    }

Hi Kityanlam3,
Your code is missing the peace that changes to primary parent object :IE the couch.
You will most likely need to change two thing.

  1. make the pillows tag not “GroundFurniture”. because they are a child object.
  2. inside your function ChangeToRed(). you need to add code here to change the parent object, you have the code to change the child but not the parent.
    furniture GameObject is the parent
    rend Renderer[ ] are child objects

something like
furniture.getcomponent().material.color = Color.red;

Hey there! Just bumped into your code; did you have any chance to fix it eventually? Could you share the changes you made?