- Home /
The question is answered, right answer was accepted
Mesh Collider doesn't work without reset
Hello, everyone!
I proceed my own mesh, and I want it interactable. But when I add a AddComponent(); in the code, it doesn’t work in the game.
If during the game I turn off and turn on the component again in the inspector panel, it works as needed.
Please, help me. I don't understand, why it happens.
P.S. Blue circle is gaze and surface collide feedback from HoloToolkit for Mixed Reality.
P.P.S. I create GameObject, create surface and add mesh collider as GameObject.AddComponent();
EDITED
GameObject go= new GameObject(name);
go.transform.SetParent(parent.transform);
MeshFilter filter = go.AddComponent<MeshFilter>();
Mesh mesh = filter.mesh;
MeshRenderer mr = go.AddComponent<MeshRenderer>();
// something
mesh.vertices = points.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
go.AddComponent<MeshCollider>();
Answer by koptehe · Sep 11, 2019 at 01:17 PM
I've solved it. I have 2 scripts. In the first, I called a function (second script) in which I created the mesh. And after that I changed my parent in the first.
Thus, you must firstly set the parent, and then create the mesh.
But why so, I did not understand (
Answer by Pangamini · Sep 09, 2019 at 02:04 PM
Do you also set the meshCollider's mesh? This is done in editor in Reset() (or when the component is added) but not when you do it in code. Just adding a meshCollider to a gameObject with a mesh renderer and filter won't automatically assign the same mesh to the collider
Yes, I add $$anonymous$$eshCollider component. I add it like this:
GameObject go= new GameObject(name);
go.transform.SetParent(parent.transform);
$$anonymous$$eshFilter filter = go.AddComponent<$$anonymous$$eshFilter>();
$$anonymous$$esh mesh = filter.mesh;
$$anonymous$$eshRenderer mr = go.AddComponent<$$anonymous$$eshRenderer>();
do something and...
mesh.vertices = points.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
go.AddComponent<$$anonymous$$eshCollider>();
$$anonymous$$eshCollider collider = go.AddComponent<$$anonymous$$eshCollider>();
collider.shared$$anonymous$$esh = mesh;
Also, access $$anonymous$$eshFilter.shared$$anonymous$$esh ins$$anonymous$$d of mesh, the latter creates a copy of the mesh
Unfortunatly, it doesn't work :( Nothing changed