Hello,
i’m generating some GameObjects in my script.
Now i want to add MeshCollider (made out of a different asset) to those GameObjects in my Script.
How do i do that?
resBlockCollision = Resources.Load("block_collision");
MeshCollider ms = block.AddComponent<MeshCollider>() as MeshCollider;
ms.sharedMesh = null;
ms.sharedMesh = (Mesh)Mesh.Instantiate(resBlockCollision);
That is what i tryed, doesn’t work for me.
If i change the assignement to:
ms.sharedMesh = Mesh.Instantiate(resBlockCollision) as Mesh;
The script compiles, but an visible mesh is generated. I just want a mesh for the meshcolider, not another visible 3D object in the game. How do i acomplish this?
Do meshRenderer component appear in game objects after you assign sharedMesh?
There are new game objects generated with meshRenderer on, the GameObject i wanted to have a meshCollider still doesn’t show a mesh assigned…
i just got started with unity3. I have a high poly 3D object and a low poly version of it for collision. I want this object to be generated per script (as i want to generate multiples of it). It should use the low poly mesh as its mesh collider. A rather simple task, but can’t figure out how this can be done in unity?
I guess Mesh.Instantiate(resBlockCollision) instantiates a new GameObject, not Mesh. That’s why (Mesh)Mesh.Instantiate(resBlockCollision) doesn’t work, and Mesh.Instantiate(resBlockCollision) as Mesh returns null.
PS
Maybe this will work, however I can’t test it right now.
resBlockCollision = (Mesh)Resources.Load("block_collision");
...
ms.sharedMesh = resBlockCollision;
Thanks, yeah tried that as well, didn’t work out.
edit: A similar question:
How do i add a new script i have in my assets to an object i generated?
gameObject.AddComponent();