- Home /
Changing texture with code
I am trying to use the following code to generate a randomized terrain texture, but what ends up happening is that Unity freezes and I have to ctrl+alt+delete in order to quit.
var tex1 : Texture; var tex2 : Texture; var plane : GameObject; var x = 4; var y = 4;
function Start() { var i; var j;
for(i = 0; i < y; i++) for(j = 0; j < x; j++) { var terrain : int = Random.Range(0,1);
Instantiate(plane, Vector3(j, 0, i), transform.rotation);
if(terrain == 1)
plane.renderer.material.SetTexture("_MainTex", tex1);
if(terrain == 2)
plane.renderer.material.SetTexture("_MainTex", tex2);
}
}
If I comment out the plane.renderer lines, then it generates a a field of purple planes. I also tried material.mainTexture = tex1. But it still causes Unity to crash. If anyone has any ideas that would be great.
I'm not exactly sure what you are doing with Random.Range, but the way you currently have it, you will only return 0. Random.Range(int $$anonymous$$ [inclusive], int max [exclusive]) : int. Hence you will only ever get 0.
yeah I noticed that, but that still doesn't stop unity from just crashing.
This code doesn't crash; check elsewhere. Also, just use material.texture.