Hey all, i’m extremely new to unity and i’m trying to create a program that will read a pixel image and then creates a set of cubes that mirrors whats in the image I have chosen
public class GridSpawner : MonoBehaviour
{
public GameObject redCube;
public GameObject blueCube;
public GameObject greenCube;
public GameObject blackCube;
public float spacing = 1.0f;
public Texture2D imageToRead;
public Color red;
public Color blue;
public Color green;
public Color black;
public Color white;
void Start ()
{
ColorCubeSpawner();
}
void ColorCubeSpawner()
{
for(int y = 0; y < imageToRead.height; y++)
{
for(int x = 0; x < imageToRead.width; x++)
{
Vector3 spawnLocation = new Vector3();
spawnLocation.x = x*spacing;
spawnLocation.y = y*spacing;
spawnLocation.z = 0;
Color[] pixels = imageToRead.GetPixels ();
foreach(Color pixel in pixels)
if(pixel == red)
{
Instantiate(redCube, spawnLocation, Quaternion.identity);
}
else if(pixel == blue)
{
Instantiate(blueCube, spawnLocation, Quaternion.identity);
}
else if(pixel == green)
{
Instantiate(greenCube, spawnLocation, Quaternion.identity);
}
else if(pixel == black)
{
Instantiate(blackCube, spawnLocation, Quaternion.identity);
}
}
}
}
}
So far that’s what I am trying to use but the problem I am running into is that it creates multiple cubes at the same location for every color and then only shows the last cube it reads overall.
Any feedback would be great. Thanks