- Home /
Level Color Map
I'm trying to create a basic level from a image I created in paint. The script is trying to get each pixel and check its color and compare it to the color I have specified, but it doesn't seem to ever pass through the if statement. var img : Texture2D; var grass : GameObject;
function Start () {
for(var x : int = 0; x < img.width; x++) {
for(var y : int = 0; y < img.height; y++) {
Debug.Log(img.GetPixel(x,y));
if(img.GetPixel(x,y) == Color(0.290, 1.000, 0.000, 1.000)) {
Debug.Log("working");
Instantiate(grass, Vector3(x, 0, y), Quaternion.identity);
}
}
}
}
The only thing I get in the Console is:(Well acctually 256 times since it's a 16x16 img) <\br><\br> RGBA(0.290, 1.000, 0.000, 1.000)<\br> UnityEngine.Debug:Log(Object)<\br> Level:Start() (at Assets/Scripts/Level.js:10)<\br>
which I got from the <\br> Debug.Log(img.GetPixel(x,y));
Answer by tanoshimi · Oct 27, 2014 at 08:48 PM
I don't see anything wrong with your code as such - I suspect that the problem comes from rounding errors in the color component values (i.e. the Red component of your pixels is not exactly equal to 0.290 - that's just the truncated value displayed by Debug.Log). As a result, your if condition is never evaluating to true.
Try it with a different image that only has white and black pixels and test for Color(0,0,0,1) and Color(1,1,1,1) - that should work.
Yea thanks, this worked. I'm not used to dealing with unity's Colors, I usually go with hexadecimals so that was the mistake.
Your answer
Follow this Question
Related Questions
Multiple Trigger Script doenst works! 1 Answer
Audio Streaming Not Working 1 Answer
help to add more details to my project 1 Answer
Why won't my character register as near? 2 Answers
Game dont work on Zenfone 5 0 Answers