Gonna spawn 3d objects from 2dTexture by checking pixel color, but if i run the script it is just randomly spawning some objects. Tried it without “if” statement and it spawned big cube. So the problem in that statement. Help pls!`
{
public Texture2D map;
public PrefabInstantiate[] ColorToPrefab;
void Start () {
map = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Maps/Map.jpg", typeof(Texture2D));
LvlGenerator();
}
void LvlGenerator()
{
for (int z = 0; z < map.width; z++)
{
for (int y = 0; y < map.height; y++)
{
GetColor(z,y);
}
}
}
void GetColor(int z,int y)
{
Color pixelColor = map.GetPixel(z, y);
if (pixelColor.r == 255 && pixelColor.b == 255 && pixelColor.g == 255 && pixelColor.a == 255)
{
return;
}
foreach (PrefabInstantiate ColorToPrefabs in ColorToPrefab)
{
if (ColorToPrefabs.color == pixelColor)
{
Vector3 spawnPoint = new Vector3(0 , y, z+25f);
Instantiate(ColorToPrefabs.prefab, spawnPoint, Quaternion.identity);
}
}
}
}
public class PrefabInstantiate
{
public Color color;
public GameObject prefab;
}