Spawn objects from picture isnt working
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;
}
Answer by victorbisaev · Jan 31, 2018 at 12:53 AM
What is the content of "ColorToPrefab"?
"Texture coordinates start at lower left corner." - does it make sense?
You mentioned "just randomly spawning some objects" - is it a problem with positions or spawned object prefabs?
Do colors in "Map.jpg" exactly match colors in "ColorToPrefab" taking into account lossy nature of JPEG format?
No idea why it spawns the "big cube".
Your answer
Follow this Question
Related Questions
How do I spawn a List of Objects so that there is a delay after each object that spawned? 0 Answers
How to change Button input to Keyboard input? 1 Answer
How do I spawn a List of Objects, so that there is a delay after each object that spawned? 0 Answers
My Character can walk on top of objects, but walks right through them 1 Answer