- Home /
Raycasthit.textureCoord not painting colored pixels
I'm a beginner to Unity and have been fiddling around with it for the last few days. The end goal for me is to create a clickable world map as described here. The instructions on that page is what led me to initially try using Raycasthit.textureCoord to paint a the clicked portion of my map a certain color.
Right now, I'm just using a 5x3x1 cube with a plain white texture attached to it. As mentioned in the documentation, I've attached a mesh collider to the cube. Also, the script with the Raycasthit.textureCoord code is attached to the camera as instructed (it is copy + pasted from the documentation). I understand for the most part what the code means, (after reading into all the variables and types), but I don't seem to understand why it's not doing what it's supposed to do (no console errors in sight).
Any help or direction would be appreciated!
Answer by robertbu · Jul 22, 2014 at 03:20 AM
I just tested the script in the reference. I found it had a couple of problems to be fixed before it would run, but it ran and worked. Note it is likely this script will make permanent changes to the texture. I use a Quad with Unlit/Texture shader and set the texture to something bright.
As for your goal, I've used similar code and it works well. The only caution is don't try and match colors exactly. Allow some leeway when detecting colors and mapping them.
What were the problems you had to fix before it worked? And what do you mean by Quad?
Thanks for the help/advice!
Line 18 got changed to this:
Texture2D tex = renderer.material.mainTexture as Texture2D;
Line 22 got changes to this:
tex.SetPixel($$anonymous$$athf.RoundToInt(pixelUV.x), $$anonymous$$athf.RoundToInt(pixelUV.y), Color.black);
Note with the second change, I could have just cast the values to integers. Actually I'm not sure why it did not automatically convert the floats to ints.
If you do Game Object > Create Other > Quad you get a Quad. A Quad is a 1 x 1, two triangle plane that is oriented vertically.
Very strange. I made the changes but the console keeps telling me to put a semicolon at the end of the Line 18 code you proposed. And I definitely have one there.
This is the error: "Assets/raycast.js(25,22): UCE0001: ';' expected. Insert a semicolon at the end."
Also, if you don't $$anonymous$$d, what's the logic behind the changes you proposed?
Thanks again.
Ahhh, you are using Javascript. I just tested the Javascript version of this script, and it ran without changes and worked for me. The changes I outlined were in the C# version of the script. Either they did a poor job of translating from Javascript to C#, or more likely something in Unity changed to require the script changes in the C# version, and the reference did not keep up.
Oops! Sorry I didn't notice earlier, new with Javascript syntax myself.
After reverting back to Javascript, I'm getting this as a cautionary warning from the console:
"Assets/raycast.js(25,53): BCW0028: WARNING: Implicit downcast from 'UnityEngine.Texture' to 'UnityEngine.Texture2D'."
(Referring to Line 18 code)
Could that possibly have something to do with it?