pixel capture, not working right.

I have a small test project where I need to capture pixel color.

Could someone please look over file and see why it is not returning the correct value?

At mouse hover it is supposed to return to a label the correct pixel value but sometimes white is returned red and red white…

This should be simple but I have no idea.603856–21455–$Awakening.zip (2.92 MB)

Thanks guys :slight_smile:

:frowning:

No ideas?

I looked over your documentation and that doesn’t even work :frowning:

Put that code in and it instantly returns errors.

Error 1 Cannot implicitly convert type ‘float’ to ‘int’. An explicit conversion exists (are you missing a cast?)
Error 2 Cannot implicitly convert type ‘float’ to ‘int’. An explicit conversion exists (are you missing a cast?)
Error 3 Cannot modify the return value of ‘UnityEngine.Transform.position’ because it is not a variable

Did you try to use the UnityScript version of the code in a C# project? Just above the code block at the mentioned link (at the right edge), you can change the displayed language. Select C# and I bet it works better…

Yep it was the C# version.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public Texture2D heightmap;
public Vector3 size = new Vector3(100, 10, 100);
void Update() {
int x = transform.position.x / size.x * heightmap.width;
int z = transform.position.z / size.z * heightmap.height;
transform.position.y = heightmap.GetPixel(x, z).grayscale * size.y;
}
}

all i did was place the 2 variables where they needed to go and the update code in the update method and it instantly returned those errors in visual studio.

Well, I opened the project and there were no errors. The position seems off somehow. I used a raycast to get the position when I did something similar.

Here is a snippet of the code I used, in case you want to try a different method. Note that this was for clicks whereas your project currently uses mouse over.

edit: I re-read your post and realized you weren’t having errors in your project. Most likely the problem is that you are using screen coordinates and you need to use texture coordinates. It’s not hard to convert them, and if you look at my code I do that conversion.

   void hitTarget()
    {
		// this script was attached to the enemy character, so it references gameObject and grabs the main texture.
		Texture2D tex = (Texture2D)gameObject.renderer.material.mainTexture;
		
		RaycastHit ray = new RaycastHit();
                                // if there isn't a current raycast, don't continue this function
		if(!Physics.Raycast(UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition),out ray))
			{  	return;    }
                                // convert screen points into texture coordinates
		Vector2 pixelUV = ray.textureCoord;
		pixelUV.x *= tex.width;
		pixelUV.y *= tex.height;
		Color hitColor = tex.GetPixel( (int)pixelUV.x, (int)pixelUV.y);
		if(hitColor.a == 1)
		{
                                       //The click was not over a transparent area.
		}
    }

I only get the errors in my current version when I put in the code from unity documentation. The file I uploaded here has no errors it just doesn’t return the value correctly.

I will look over yours, thank you :0

Ah, yeah, looking closer, there are definitely some problems with the sample code, which should probably be reported as a doc bug.

Anyway, you’ll need to cast the results of the math to an int before assigning it to the x and z vars. Also, the transform position can’t be changed as shown in C#. Here’s some info on that:

http://forum.unity3d.com/threads/60887-C-transform?highlight=UnityEngine.Transform.position

Jeff