x


Trying to access a texture's pixel coords.

I'm attempting to access a texture's pixel coord's to pull out what color is at that specific coords. Right now I am firing a raycast at an specific object (in this case a laptop) pulling off the objects textureCoords, then looking those coords up on a stored texture "laptopstatic_handling" in the Resources folder. I am getting a Unity error that says:

UnityException: Texture 'laptopstatic_handling' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings. RayCastController.Update () (at Assets/RayCastController.cs:35)

Not quite sure what that means. Here is the sample of code

// Use this for initialization
void Start () {
    tex = (Texture2D)Resources.Load("laptopstatic_handling");
}

// Update is called once per frame
void Update () {

    RaycastHit ray = new RaycastHit();
    if(!Physics.Raycast(UnityEngine.Camera.current.ScreenPointToRay(Input.mousePosition),out ray)){
        return;
    }

    name = ray.collider.gameObject.name;

    Renderer renderer = ray.collider.renderer;
    MeshCollider meshCollider = (MeshCollider)ray.collider ;

    //Check to see the collider has a renderer and texture
    if (renderer == null || renderer.sharedMaterial == null ||
        renderer.sharedMaterial.mainTexture == null || meshCollider == null){
        return;
    }


    Debug.Log( tex.GetPixel( (int)ray.textureCoord.x,(int)ray.textureCoord.y).ToString());
more ▼

asked Aug 24 '10 at 03:14 PM

Vince gravatar image

Vince
199 7 8 16

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Well, why not simply do what the error message suggests: go to the texture "laptopstatic_handling" in the Project tab, then in the Inspector enable "IsReadable" in its import settings. From the GetPixel() docs:

"The texture must have the Is Readable flag set in the import settings, otherwise this function will fail."

more ▼

answered Aug 24 '10 at 03:41 PM

Wolfram gravatar image

Wolfram
9.1k 8 20 53

I completely skipped over that sentence. Thanks, works now.

Aug 24 '10 at 03:44 PM Vince

Uhm, I'm getting crazy here. Using Unity 3.5, I can't find anywhere the isReadable checkbox in the textures Inspector... What am I doing wrong?

Mar 15 '12 at 08:03 PM Izitmee

You can find this Checkbox by choosing "Advanced" in the "Texture Type comboBox.

It is called "Read/Write Enabled"

Mar 24 '12 at 03:11 PM cosmogonies

If you add the steps to your answer to change the mesh + change the material to "Advanced" and enable read/write I will be very happy to accept your answer.

Thanks so much!

Feb 24 at 12:03 PM mrpmorris
(comments are locked)
10|3000 characters needed characters left

You're looking for "Is read/write Enabled"

more ▼

answered Mar 18 '12 at 10:13 PM

Redlazer gravatar image

Redlazer
61 7 9 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2279
x1580
x344
x88

asked: Aug 24 '10 at 03:14 PM

Seen: 8535 times

Last Updated: Feb 24 at 12:03 PM