So I have this sofa that has multiple gameobjects within it, notably cushions on top of it.
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;
}
}