The question is answered, right answer was accepted
Changing Second Material
Hi! I have a object with 2 materials on it, and I want to change the second material via code. This is the code I'm using:
public class ChangeColorGreen : MonoBehaviour {
public PlayerScript playerScript;
public Material[] materials;
public Renderer rend;
void Start () {
}
void Update () {
if (playerScript.isGreen == true) {
rend.sharedMaterials[1] = materials[0];
}
}
}
When I attempt to change the second material, absolutely nothing happens:
But when I change the code to
rend.materials[1] = materials[0];
This happens:
But I can change the first element perfectly fine: http://i.imgur.com/n5kHuLf.png?1 (changed to imgur becuase the site stopped me from uploading pictures)
So does anyone have an insight on this? Thanks!
Answer by Namaarie · Apr 26, 2016 at 12:42 AM
I have managed to figure this out by myself, changed the code to
rend.materials [1].color = materials [0].color;
and it works perfectly fine. Use
rend.sharedMaterials[1].color = materials [0].color;
if you want the color change to stay permanently.
Follow this Question
Related Questions
Changing Texture after time has passed 1 Answer
Drawing multiple materials from a defined array on a single object 0 Answers
Time delay enemy respawn 3 Answers
Delay in time 0 Answers