# Shader wants tangents, but the mesh doesn't have them

I have generated some meshes via script and they load and work correctly in game, however I still get the above warning.
How do I add tangents to fix this issue?
I am using unity 4.2

You either use a shader that doesn’t require tangents, or you supply tangents to your generated mesh. Tangents are typically required when the shader uses bump mapping, so pick one that isn’t bump mapped. If you feel it looks correct without it, then you don’t need bump mapping anyway.

The second option is to add the tangents to your mesh that the shader is asking for. There’s a method that does that for you automatically, I found it online some time ago but I can’t remember where exactly, so I’ll just copy-paste it here (C#):

``````private static void TangentSolver(Mesh theMesh)
{
int vertexCount = theMesh.vertexCount;
Vector3[] vertices = theMesh.vertices;
Vector3[] normals = theMesh.normals;
Vector2[] texcoords = theMesh.uv;
int[] triangles = theMesh.triangles;
int triangleCount = triangles.Length / 3;
Vector4[] tangents = new Vector4[vertexCount];
Vector3[] tan1 = new Vector3[vertexCount];
Vector3[] tan2 = new Vector3[vertexCount];
int tri = 0;
for (int i = 0; i < (triangleCount); i++)
{
int i1 = triangles[tri];
int i2 = triangles[tri + 1];
int i3 = triangles[tri + 2];

Vector3 v1 = vertices[i1];
Vector3 v2 = vertices[i2];
Vector3 v3 = vertices[i3];

Vector2 w1 = texcoords[i1];
Vector2 w2 = texcoords[i2];
Vector2 w3 = texcoords[i3];

float x1 = v2.x - v1.x;
float x2 = v3.x - v1.x;
float y1 = v2.y - v1.y;
float y2 = v3.y - v1.y;
float z1 = v2.z - v1.z;
float z2 = v3.z - v1.z;

float s1 = w2.x - w1.x;
float s2 = w3.x - w1.x;
float t1 = w2.y - w1.y;
float t2 = w3.y - w1.y;

float r = 1.0f / (s1 * t2 - s2 * t1);
Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);

tan1[i1] += sdir;
tan1[i2] += sdir;
tan1[i3] += sdir;

tan2[i1] += tdir;
tan2[i2] += tdir;
tan2[i3] += tdir;

tri += 3;
}

for (int i = 0; i < (vertexCount); i++)
{
Vector3 n = normals*;*
``````

Vector3 t = tan1*;*

// Gram-Schmidt orthogonalize
Vector3.OrthoNormalize(ref n, ref t);

tangents*.x = t.x;*
tangents*.y = t.y;*
tangents*.z = t.z;*

// Calculate handedness
tangents_.w = (Vector3.Dot(Vector3.Cross(n, t), tan2*) < 0.0) ? -1.0f : 1.0f;
}
theMesh.tangents = tangents;
}*