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.

alt text

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.
alt text

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?

–

1 Answer

1

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.