Reputation: 148
i have 2 Materials on one gameObject and i want to change the color of the 2. with a c# script
But this script only change the 1. one and i want to change the 2.
Kind regards
rayHit.collider.gameObject.GetComponent<MeshRenderer>().material.color = Color.black;
Upvotes: 0
Views: 274
Reputation: 1324
If you use GetComponent<MeshRenderer>().material
, you change the first material.
You need to use GetComponent<MeshRenderer>().materials
to get all materials of your MeshRenderer
in an array.
So if you want to modify the second material, you need to modify the index 1 of the materials array like that :
rayHit.collider.gameObject.GetComponent<MeshRenderer>().materials[1].color = Color.black;
Upvotes: 2