- Home /
Question by
Jermainek369 · Apr 10, 2016 at 11:59 AM ·
instantiateimagegameobjectspixel
I am trying to spawn coloured cubes that match a pixel image. At the moment the cubes spawn but i do not know how to match up the colours.
{
public GameObject objToSpawn;
public Texture2D imageToRead;
public int xSize, ySize;
public float spacing = 1.5f;
void Start ()
{
SpawnMethodOne();
}
void SpawnMethodOne()
{
for(int y = 0; y < imageToRead.height; y++)
{
for(int x = 0; x < imageToRead.height; x++)
{
Vector3 spawnLocation = new Vector3();
spawnLocation.x = x*spacing;
spawnLocation.y = y*spacing;
spawnLocation.z = 0;
Instantiate(objToSpawn, spawnLocation, Quaternion.identity);
}
}
}
} This is what i have at the moment, how do i make the cubes match the colours?
Comment
You can use GetPixel or GetPixels to read the color of a pixel from an image. The image file has to be set to Read/Write enabled in the import settings. You can then use this color to set the cube's material's color.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074541im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
inspector panel blank 0 Answers
extra GameObjects Spawning 2 Answers
Make a game object in a scene into a prefab with script 1 Answer
Instantiating Multiple Prefabs at Runtime 0 Answers
Cloned objects not appearing in game while in play mode 1 Answer