Texture shows up in Inspector but object is red without texture

This is a problem I’ve come across a couple of times already:

I’m loading .obj files and their .jpg textures from HDD at runtime (not as Unity assets!) and even though the GameObject shows up in the scene, there’s no texture and the object’s simply red. It is possible to change the color of the object with “myMaterial.color = Color.cyan” but the actual texture isn’t mapped onto the object. If I click on the object in the “scene” tab, the texture shows up in the Inspector on the lower right (it’s textured on a sphere).

Just a heads up: I did manage to solve it in the end but since not even Google knew an answer to it, I’m going to post the answer - hopefully it’ll help someone at one point.

The reason why this is happening are the UVs. Either they are missing completely or they are invalid in some other way. In my case the array had the right length but was filled with zeroes.

Important: You have to set the UVs AFTER setting the vertices, otherwise there’s an exception that the UV array has to be the same length as the vertices array of that mesh (even though the vertices aren’t even set yet).