- Home /
Applying different textures to spawned objects.
I'm instantiating a grid of cloned objects and need to apply a different texture to each depending on the values in a multidimensional array from a script attached to the main camera. I'm a bit rusty and can't remember how to give each instantiated object the appropriate texture. Can someone please help me out?
for (var y = 0; y < gridY; y++) { for (var x=0;x < gridX; x++) { var pos = Vector3 (x + gridOffset, 0, y + gridOffset) * spacing; currTile = GS_Games[gameNum, y, x];
// GS_Pics[currTile] holds the material to be applied
Instantiate(GS_Tile, pos, Quaternion.identity);
}
}
In the end I created the objects off camera in the editor and cloned each one as needed. It's a cludgy way of doing it though.
Answer by DaveA · Jan 15, 2011 at 02:27 AM
Something on the order of
currTile.renderer.material.mainTexture = TGS_Pics[currTile]; // if TGS_Pics are textures
currTile.renderer.material.mainTexture = TGS_Pics[currTile].mainTexture; // if TGS_Pics are materials and you just want to transfer the texture
Each tile is being rendered with a pictures based on an array. currTile is an integer with the reference, hence GS_pics[currTile] pointing to the material I need to use. I tried instantiated with an array of Gameobjects but something seems to be wrong with the following line when I try that. GS_Tiles[currTile] = Instantiate(GS_Tile, pos, Quaternion.identity);
Turned out I simply forgot to add " as GameObject" at the end of the command. :P
Answer by Jessy · Jan 15, 2011 at 02:29 AM
This is for multiple objects in a grid that are spawned from script. $$anonymous$$inimum of 81 for a small puzzle to a max of 324 for a large one, some using text, some with numbers, some with slices of a picture etc.. I can't figure how to reference them to change the materials after they are instantiated. Brain stalled. :P