Trying to create cubes accoriding to a pixel image

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

sorry, I change a lot from your code, and don’t if it is what you want

using UnityEngine;

public class GridSpawner : MonoBehaviour
{
    public Texture2D imageToRead; // notice to set image being readable
    private GameObject[] imageWall;

    
    void Start()
    {
        ColorCubeSpawner();
    }
    

    void ColorCubeSpawner()
    {
        Color[] pixels = imageToRead.GetPixels();
        imageWall = new GameObject[pixels.Length];

        for (int index = 0; index < pixels.Length; index++)
        {
            imageWall[index] = GameObject.CreatePrimitive(PrimitiveType.Cube);
            imageWall[index].transform.position = new Vector3(index % imageToRead.width, index / imageToRead.width, 0);
            imageWall[index].GetComponent<MeshRenderer>().material.color = pixels[index];
            
        }
        
    }

}