How do you assign a material to multiple different objects using arrays?
Hey there,
In my unity project I've got an array of 16 gameobjects and an array of 16 bools. I would like to make a function that when called will check the bools and give all false bools a purple material and all true bools a blue material.
This means that if I would set the first bool of the bool array to false and then call the function, the material of the first gameobject would be set to purple, but if the first bool would be true, the material of the first gameobject should be blue. And so on for all 16 gameobjects of the array.
Does someone know how to do this? (I'm kinda new to C# and arrays)
Answer by Ali-hatem · Apr 24, 2016 at 02:06 PM
if it was about only to colors this function is good . if not then script should be extended so let me know.
public GameObject[] obj;
public bool[] cheek;
Renderer rn;
void Change(){
for (int i = 0; i < obj.Length; i++) {
rn = obj[i].GetComponent<Renderer> ();
if (cheek [i]) {
rn.material.color = Color.blue;
} else {
rn.material.color = Color.magenta;
}
}
}
Your answer
Follow this Question
Related Questions
Changing color in in runtime to a cube 0 Answers
How to only access one object in array if it's index is 1 0 Answers
unity C# Errors `UnityEngine.Material' does not contain a definition for `SetColorArray' 0 Answers
Problem with Asset Bundles in Unity 2020.3.30f1 1 Answer
can i apply an ad on a material 1 Answer