- Home /
How do you change the tint of an entire material array for differing objects?
So I have multiple objects sharing one script. Part of this script is used to change the tint of the objects that have it attached. Here's the script portion I'm talking about:
var materials : Material[];
function Start ()
{
materials = renderer.materials;
materials[0].color = new Color(0.5, 0.5, 0.5);
materials[1].color = new Color(0.5, 0.5, 0.5);
materials[2].color = new Color(0.5, 0.5, 0.5);
materials[3].color = new Color(0.5, 0.5, 0.5);
materials[4].color = new Color(0.5, 0.5, 0.5);
materials[5].color = new Color(0.5, 0.5, 0.5);
materials[6].color = new Color(0.5, 0.5, 0.5);
materials[7].color = new Color(0.5, 0.5, 0.5);
materials[8].color = new Color(0.5, 0.5, 0.5);
materials[9].color = new Color(0.5, 0.5, 0.5);
materials[10].color = new Color(0.5, 0.5, 0.5);
materials[11].color = new Color(0.5, 0.5, 0.5);
materials[12].color = new Color(0.5, 0.5, 0.5);
materials[13].color = new Color(0.5, 0.5, 0.5);
materials[14].color = new Color(0.5, 0.5, 0.5);
materials[15].color = new Color(0.5, 0.5, 0.5);
}
function Update ()
{
if (owned)
{
materials[0].color = new Color(1, 1, 1);
materials[1].color = new Color(1, 1, 1);
materials[2].color = new Color(1, 1, 1);
materials[3].color = new Color(1, 1, 1);
materials[4].color = new Color(1, 1, 1);
materials[5].color = new Color(1, 1, 1);
materials[6].color = new Color(1, 1, 1);
materials[7].color = new Color(1, 1, 1);
materials[8].color = new Color(1, 1, 1);
materials[9].color = new Color(1, 1, 1);
materials[10].color = new Color(1, 1, 1);
materials[11].color = new Color(1, 1, 1);
materials[12].color = new Color(1, 1, 1);
materials[13].color = new Color(1, 1, 1);
materials[14].color = new Color(1, 1, 1);
materials[15].color = new Color(1, 1, 1);
}
}
So the objects are supposed to start out dark, and when 'owned' becomes true, they become lighter. This actually works. I have no problem getting the objects to change their tint. My problem is that not all of the objects have the same amount of materials, most of them less than 15. Because of this I am getting the error, IndexOutOfRangeException: Array index is out of range. So I know why I'm getting the problem, but I was hoping there was a better way of writing my code, so it would only affect the amount of materials that are actually on each individual game object. I guess what I'm askingis that, is there a way to say something like, materials[allMaterialsOnThisObject].color = new Color(0.5, 0.5, 0.5), instead of having that huge block of code for each material in the array?
The game still works even with these errors, they just bother me. I'm sure there are other ways to circumvent this error, like having each script individualized to get the proper amount of materials for each object. I just wanted to know if anyone knew of a way to do what I was suggesting.
Thanks in advance.
Never hard-code a bunch of array entries like that; use a loop.
Answer by cagezero · Apr 26, 2013 at 01:35 AM
As Eric5h5 suggested, try using a loop:
for(int x = 0; x < materials.Length; x++) {
materials[x].color = newColor;
}
Your answer
Follow this Question
Related Questions
How to change object's material while preserving material's color? 0 Answers
Texture Brightness is darker when Loading from another Scene 0 Answers
How to set color to gameObject respective to below coding? 2 Answers
Fade In _TintColor Over Time 2 Answers
Brightening the colour of a material that can be any colour without shaders. 1 Answer