Tiling textures differently depending on prefab instance size

I’m building a maze. The walls are built from a single prefab. They will all be 1xN, where N is a whole number. The problem is that I want to be able to tile the texture on the sides of the wall a number of times proportional to N. But changing this will change it for the master texture, hence on all the prefabs.

Maybe I’m doing this incorrectly, and the best way is just build walls by lining up multiple instances of the prefab. This just seems to be inefficient to me, having more objects then is necessary.

So should I build the walls out of multiple objects? If not, how do I tile the texture on a per wall basis?

The trick to doing the tiling is to change the instances, not on the prefab. When you make a change on the instances, Unity generates a new material instance. Create a prefab with a texture that will show its tiling (and where the texture is named ‘_MainTex’ in the material), and attach this script:

var goPrefab : GameObject;

function Start() {
	var go : GameObject = Instantiate(goPrefab, Vector3.zero, Quaternion.identity);
	go.renderer.material.SetTextureScale("_MainTex", Vector2(2,2));
	go = Instantiate(goPrefab, Vector3.up * 2, Quaternion.identity);
	go.renderer.material.SetTextureScale("_MainTex", Vector2(4,4));

You will see that the two different instances of the same prefab get their own tiling.

If your walls are simple planes (not boxes) and you don’t need to see inside them, you can use Tiled Sprite that will automate the tiling: Unity Asset Store - The Best Assets for Game Making

Making each wall tile an individual object would most probably introduce a large performance hit.