instanced mesh won't collide with raycast anymore

Hello.

I have a connect the dots game where you click three times (x,y) to generate a triangle with the vertices set as the three points of click. It generates the triangle fine and it used to collide fine. I ran into the problem (lots of problems) along the way of only one side being able to collide whether you clicked clockwise, or counter-wise, so now I have it setup to instance two completely separate meshes, one facing front, one facing back. Then I raycast from the “treasure” points straight out to see if they collide with the only collision possible objects, the meshes.

—It used to work. I rewrote everything so that I wasn’t using just precalled variables, but now everything is lists so that eventually the player can level up and make more clicks/triangles/etc. Everything else works fine after conversion to lists, except for triangle collision.

—If I turn on the back wall’s collider, all treasure points will hit it just fine; which means the raycasting and collision detection are working fine. for some reason it won’t hit the meshes though.


???

I can see their MeshRenderers just fine and they are applied the same mesh as the MeshCollider. It makes officially no sense to me anymore.

Please help.

note: the script below is from the main camera where the commands are given, and then from the mesh prefabs that are instanced and have their own set of vertices. I’ve since rewritten them to single lines/private/1,000 other ways. still not registering.??

	bool CalcHit(int num) {
		RaycastHit hit = new RaycastHit();
		Ray ray = new Ray();
		ray.origin = new Vector3(dots[num].transform.position.x, dots[num].transform.position.y, 50);
		ray.direction = new Vector3 (0, 0, -1);
		if (Physics.Raycast(ray, out hit, 51.0f)) return true;
		else return false;
	}

	public Vector3[] _vertices;
	public Vector3[] _normals;
	public Vector2[] _UV;

	public void SetTrianglePosition(Vector3 pos1, Vector3 pos2, Vector3 pos3){
		_vertices [0] = pos1;
		_vertices [1] = pos2;
		_vertices [2] = pos3;
		_UV [0] = new Vector2(pos1.x, pos1.y);
		_UV [1] = new Vector2(pos2.x, pos2.y);
		_UV [2] = new Vector2(pos3.x, pos3.y);
	}
	
	public void CreateTriangle() {
		Mesh mesh = new Mesh();
		GetComponent<MeshFilter>().mesh = mesh;
		GetComponent<MeshCollider>().sharedMesh = mesh;
		mesh.Clear();
		mesh.vertices = _vertices;
		mesh.uv = _UV;
		mesh.triangles = new int[] {0, 1, 2};
		mesh.normals = _vertices;
	}

In trying out stevethorne’s idea I was moving lines around a bit. the prefab is already null, so I didn’t get any results from adding a line to specifically Clear() or = null; the sharemesh, but changing the order of the code solved the problem:

    public void CreateTriangle() {
       Mesh mesh = new Mesh();
       mesh.Clear();
       mesh.vertices = _vertices;
       mesh.uv = _UV;
       mesh.triangles = new int[] {0, 1, 2};
       mesh.normals = _vertices;
       GetComponent<MeshFilter>().mesh = mesh;
       GetComponent<MeshCollider>().sharedMesh = mesh;
    }

it used to work in that order a couple weeks ago before I rewrote everything… so I’m not sure why it’s gotten fussy about order, but logically I always thought it should be like this… had copied the basic script from a different answer on here and was using it for several weeks.

now it’s applying the GameObject’s MeshCollider.sharedMesh after the mesh’s vertices are defined and it is hitting properly…?? there seems to be no change with the MeshFilter.mesh, but I’m just glad it’s fixed.