I have a plane with a mesh renderer attached which has an array of two materials, m_Ball (Element 0) and m_BallHighlight (Element 1.) The tint of m_Ball is currently red. I'm trying to write a function in c# that can change the tint of Element 0 to a different tint at a defined time during run time but I don't know how to do it. I've tried typing various keywords and seeing what options come up after the dot but to no avail. Any help would be much appreciated.
asked Dec 13 '10 at 12:33 PM
When dealing with a Material array, and you want to change an index of that array, you need to actually reassign the entire array for it to recognize the change.
So here I created a new Material and assigned a new Material to index 0, but used a reference to the current index 1 of our renderer.materials array.
That is very generic code, so you may need to customize it to your needs but the principle would be the same in getting it to recognize that the array has changed.
Hope that helps
answered Dec 13 '10 at 03:07 PM
I'm sorry if I wasn't being clear. As a newbie you're always worried about giving too much info or not enough.
I have a ball object with script, collider, rigid body etc. attached. It also has a Plane mesh with a Mesh Renderer attached. The Mesh Renderer has a 2 element array to hold materials. Both are of shader type Particles/Alpha Blended so I don't have to worry about lighting. Element 0 is called m_Ball. It has a texture map which was originally greyscale but I've set the Tint Color to a red color - that's what I want to change when the game is played. Element 1 is called m_BallHighlight. Element 1 won't change; it's a texture map with an alpha channel which sits on top of the main texture map so I can have highlights that aren't affected by the underlying Tint in Element 0.
When the game is playing I have many different coloured balls in play. I want to change the tint of element 0 of any particular ball to one of several different colors. So I've created a public ChangeColor() function inside the Ball Script and will probably pass in one of 7 colors depending on what it's collided with.
I'm guessing that my first task is to access Element 0 of the Materials array which is on the Mesh Renderer Component attached to the Plane Mesh, which is a child of the Ball Prefab. Then I have to change it. Do I do that by entering rgb values 0-255 or 0-1.0. or do I define a series of materials in Red, Blue, Yellow etc and swap the material in element 0 for them, & will that affect all the other instances of the Ball prefab?
I hope I've made my problem more clear instead of burying it under too much detail. Thank you for the replies so far. I do appreciate them.
answered Dec 13 '10 at 04:43 PM