Hello, I though I would get answer/help here quicker than in Ask Question-section since I guess this is not really a question.
So I need help with my script since I have many mesh renderers in my project with i.e. 7 elements and I need to change second and third elements to different material by simply pressing GUI-button and other materials needs to stay as such.
Here is part of the script where you can see main structure of it:
var Part : GameObject;
var Part2 : GameObject;
var Part3 : GameObject;
var Part4 : GameObject;
var mat2 : Material;
var mat3 : Material;
var mat4 : Material;
var mat5 : Material;
var matD : Material;
function OnGUI()
{
if(GUI.Button(Rect(120, 40, 80, 20),"Black"))
{
Part.GetComponent.<Renderer>().material = mat2;
Part2.GetComponent.<Renderer>().material = mat2;
Part3.GetComponent.<Renderer>().material = mat2;
Part4.GetComponent.<Renderer>().materials[3] = mat2;
}
As you can see I’ve tried materials[3] as to choose that part third element, but it doesn’t work. Script itself continues from that point with different materials for different GUI-button, so I guess those are not relevant to be here.
Thanks for your reply.
But if I can’t change it in the way I’ve tried, I would appreciate if someone could help me to create working solution for it, because I really need to be able to change materials of those certain parts/elements by click of a gui-button.
I don’t think you need to work that hard just to change a material though… your code is just doing nothing? the way the API is worded you do need to do that I guess… pull it out, change it, put it back
Even if the thread is old, this should get you in the right direction. You do have to like store the current array, change your desired materials and then replace the original materials array with a “duplicated” one
Well I’ve tried to wrap my brain around this and I don’t seem to understand how I am going to integrate that @malkere piece of code into my code. I’ve tried putting it here and there but it doesn’t work and I’m pretty sure that’s all because of my coding skills aren’t really efficient. So if you could provide little more help I would appreciate that. And to mention I use javascript in this part and provided code is c#.
I’m sorry I’m not familiar with Javascript personally.
What are you having trouble understanding? What the API is stating is that you cannot -access- the material array directly. If you call on it a copy will be created for you to look at. Thus, no matter what you do to it it will not change as you’re only changing a copy.
So, you have to swallow that and 1, take the copy → 2, change the copy → 3, then tell the original array to become the copy you’ve changed. That’s what the code I wrote up is doing in lines 1, 2, and 3.
I’m sure JS writes it the same order, just slightly different wording.
Does that make sense?
But with your script you only change the color, how do you change the materials? Materials Element [0], Element [1], Element [2], Element [3], Element [4], Element [5]