x


How to change the color of a material in an array at run time

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.

more ▼

asked Dec 13 '10 at 12:33 PM

Simon Foster gravatar image

Simon Foster
26 3 3 7

are you ok with JS?

Dec 13 '10 at 12:40 PM Kourosh
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

renderer.materials = new Material[2]{new Material(Shader.Find("MyShader")),renderer.materials[1]};

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

==

more ▼

answered Dec 13 '10 at 03:07 PM

equalsequals gravatar image

equalsequals
4.5k 16 28 64

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 13 '10 at 04:43 PM

Simon Foster gravatar image

Simon Foster
26 3 3 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1358
x813
x506

asked: Dec 13 '10 at 12:33 PM

Seen: 1668 times

Last Updated: Dec 13 '10 at 12:33 PM