- Home /
How do I texture objects in an array with different textures?
Hi, I am currently learning how to use unity and I am learning how to make a puzzle piece game. I have stored all the pieces in an array and have labeled them. But now I don't know how to actually texture each piece individually. Here is my code so far:
using UnityEngine;
using System.Collections;
public class renderTexture : MonoBehaviour
{
private GameObject[] blocks = new GameObject[15];
void Start()
{
Texture image = (Texture2D) Resources.Load("Cuzz", typeof(Texture));
blocks[0] = GameObject.FindGameObjectWithTag("Cube01");
blocks[1] = GameObject.FindGameObjectWithTag("Cube02");
blocks[2] = GameObject.FindGameObjectWithTag("Cube03");
blocks[3] = GameObject.FindGameObjectWithTag("Cube04");
blocks[4] = GameObject.FindGameObjectWithTag("Cube05");
blocks[5] = GameObject.FindGameObjectWithTag("Cube06");
blocks[6] = GameObject.FindGameObjectWithTag("Cube07");
blocks[7] = GameObject.FindGameObjectWithTag("Cube08");
blocks[8] = GameObject.FindGameObjectWithTag("Cube09");
blocks[9] = GameObject.FindGameObjectWithTag("Cube10");
blocks[10] = GameObject.FindGameObjectWithTag("Cube11");
blocks[11] = GameObject.FindGameObjectWithTag("Cube12");
blocks[12] = GameObject.FindGameObjectWithTag("Cube13");
blocks[13] = GameObject.FindGameObjectWithTag("Cube14");
blocks[14] = GameObject.FindGameObjectWithTag("Cube15");
blocks[0].renderer.material.mainTexture = image;
}
void Update()
{
}
}
Cuzz is the name of the image I am trying to put on.
So does blocks[0].renderer.material.mainTexture = image;
set the texture for the "Cube01" object?
$$anonymous$$aybe change you line 10 on:
Texture2D image = (Texture2D) Resources.Load("Cuzz", typeof(Texture));
No worries. I figured it out. See, the block that was assigned to block[0] didn't have the proper tag. I'm telling the program to find the tag "Cube01" but there wasn't one. The block that I wanted labeled as "Cube01" was ins$$anonymous$$d tagged with "Cube". Which is why I kept getting an error. All the other blocks were labeled correctly. Only block[0] wasn't.
Answer by zharik86 · Jul 27, 2014 at 06:29 AM
Most likely for all your cubes you use one material Diffuse based on a standard shader. Therefore when you change a texture in block[0], you change a texture of a material and according to all remaining blocks. The simplest in this situation, it to make the material on each block. But you remember that it will increase draw calls (dynamic batching won't work). There is the second way, to make one material for all blocks and when you change in it a texture, create one more material, being based on the main.