tim
tim

Reputation: 148

Unity3D gameObject Color Change?

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

Answers (1)

AlexAR
AlexAR

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

Related Questions