- Home /
Why does the same material render differently here?
I have two examples.
The first demonstrates a prefab with two materials (a checker pattern and a green outline) added to it's mesh renderer. The result is as expected.
The second prefab includes only one material and has the other material added at runtime. This however, led to some unexpected artifacts surrounding the newly added material.
Here is the source:
public class Init:MonoBehaviour
{
private Object fine;
private Object crap;
private GameObject[,] blocks;
private Material highlight;
void Start () {
highlight = (Material)Instantiate(Resources.Load("Materials/Highlight"));
fine = Resources.Load("Prefabs/Fine");
crap = Resources.Load("Prefabs/Crap");
blocks = new GameObject[10,10];
for(int i = 0; i <= blocks.GetUpperBound(0); i++)
{
for(int j = 0; j <= blocks.GetUpperBound(1); j++)
{
/*
// This looks fine.
blocks[i, j] = (GameObject)Instantiate(fine);
blocks[i, j].transform.position = new Vector3(i, -0.5f, j);
/*/
// This looks like crap.
blocks[i, j] = (GameObject)Instantiate(crap);
blocks[i, j].transform.position = new Vector3(i, -0.5f, j);
Material[] m = new Material[2];
m[0] = blocks[i,j].renderer.materials[0];
m[1] = highlight;
blocks[i, j].renderer.materials = m;
//*/
}
}
}
}
Finally, the entire project can be downloaded here.
Thanks for reading.
What version of unity is that project using? I was unable to open it :(
Also, it looks like you are loading two different prefabs. Are they supposed to be the same exact asset? Or why did you call one "Fine" and another "crap"? What makes them differ?
@ThePunisher Sorry, it was upgraded to 4.3. And I separated them into two different prefabs to illustrate my point.
Any luck yet? What happens if you don't instantiate the material and you just load it and set it?
Answer by flixilplix · Nov 16, 2013 at 03:03 AM
If you get a chance to open the project, you'll see the second material is transparent and thus needs to be the second one applied to the mesh.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122003im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
CombineMeshes with Different Materials 8 Answers
Applying Theme to game ? 0 Answers
Sprite disappears after adding a material to it 1 Answer
See through issue in iOs 0 Answers