Texture Coordinate Build problem. Need help T_T

Hi, I’ve meet some issues about texture output? Sorry I haven’t identified the cause yet and I don’t know where it’s arriving. I made a simulation of ray scanning, where an emitted ray that collides with an object’s collider reads the color of the corresponding texture’s UV coordinates and generates a dot of the same color. This is some part of the logic of my code:

  Texture2D texture = renderer.material.mainTexture as Texture2D;
  if (texture != null)
  {
      Vector2 pixelUV = hit.textureCoord;
      pixelUV.x *= texture.width;
      pixelUV.y *= texture.height;
      Color pointColor = texture.GetPixel((int)pixelUV.x, (int)pixelUV.y);
      Vector3 adjustedHitPoint = hit.point;
      CreateNode(adjustedHitPoint, pointColor);
  }

Afterwards when I tested it within unity everything worked fine and the scene mapping and scanned colors were all correct.
Then after I build it, the original scene is still fine, but the scanned scene can’t get the colors (I think it’s all just reading one same coordinates, because I changed to another object, it is also one color.), like this:


Top: the original scene, which is the object I will “scan”.
Middle: the Scan scene in unity test(play), which is the function I want successfully.
Bottom: the Scan scene after build, the problem I want to solve. All the places that should have been the color of middle Figure have turned black.

I checked online and asked chatgpt and other AIs. I’m sure that the texture setting is Enable Read/Write and the texture format setting is RBGA 32bit and the texture image size is fine. The texture path should be fine, I have it in the Material folder in Assets.

So I would like to ask what could be the problem that the UV coordinates are not read correctly after build?

Thanks! :smiling_face_with_tear: