Code mesh is not being set

Hi!

I’ve created a mesh in code but when I spawn in my object, the mesh is not set on the object.

Here’s my code:

void CreateBridge(Vector3 bridgeStart, Vector3 bridgeEnd){
		float width = 2;
		float lenght = Vector3.Distance(bridgeStart, bridgeEnd);

		GameObject theBridge = (GameObject)Instantiate(bridge);

		bridge.transform.rotation = Quaternion.FromToRotation(Vector3.right, bridgeEnd - bridgeStart);

		bridge.transform.position = bridgeStart + new Vector3(0, 0.01f, 0);

		MeshFilter mesh_filer = bridge.GetComponent<MeshFilter>();

		Vector3[] vertices = {
			new Vector3(0, 		0,	-width/2),
			new Vector3(lenght,	0, 	-width/2),
			new Vector3(lenght, 0,	width/2),
			new Vector3(0, 		0, 	width/2)
		};

		int[] triangles = {
			1, 0, 2,
			2, 0, 3
		};

		Vector2[] uv = {
			new Vector2(0, 0), 
			new Vector2(1, 0), 
			new Vector2(1, 1), 
			new Vector2(0, 1)
		};

		Vector3[] normals = {
			Vector3.up,
			Vector3.up,
			Vector3.up,
			Vector3.up
		};

		Mesh theMesh = new Mesh();

		theMesh.vertices = vertices;
		theMesh.triangles = triangles;
		theMesh.uv = uv;
		theMesh.normals = normals;
		theMesh.name = "Bridge";

		mesh_filer.mesh = theMesh;

		bridge.transform.position = new Vector3(bridge.transform.position.x, bridge.transform.position.y - 0.05f, bridge.transform.position.z);
	}

Thanks in advance!

you are instantiating into variable theBridge, but after that you are using bridge (the prefab, not the instantiated gameObject) :

GameObject theBridge = (GameObject)Instantiate(bridge);
bridge.transform.rotation = Quaternion.FromToRotation(Vector3.right, bridgeEnd - bridgeStart);
bridge.transform.position = bridgeStart + new Vector3(0, 0.01f, 0);
MeshFilter mesh_filer = bridge.GetComponent<MeshFilter>();
etc etc

so from line 7 and onwards, replace every mention of bridge. with theBridge. (this is where a better naming convention would be handy ie. use bridgePrefab instead of just bridge, then you can see it clearly)