- Home /
How to assign Icosphere to meshFilter with script?
Hi :)
I am creating helper game objects via script and I would like to be able to assign some of Unity's lower poly default meshes to their meshFilter components, like the Icosphere or Icosahedron.
GameObject helperGameObject = new GameObject ("HelperGameObject");
MeshFilter meshFilter = helperGameObject.AddComponent<MeshFilter>();
meshFilter.mesh = *insert code to access Icospere mesh*;
Thanks :)
As you can't use the CreatePrimitive method to get those primitives, I don't think you'll be able to do it purely from script, you could have a public mesh field on your script or even an array of meshes and assign the primitive from the inspector then use this in your code though, it's not purely with script as it relies on you assigning the primitives with the inspector but probably the simplest solution.
Thanks Phles, that sounds like the best solution so far :)
Answer by nesis · Feb 10, 2014 at 06:42 AM
I'd grab it by doing something like this (in C#):
GameObject temp = GameObject.CreatePrimitive(PrimitiveType.Sphere);
meshFilter.sharedMesh = temp.GetComponent<MeshFilter>().sharedMesh;
Destroy(temp);
Thanks for the reply but I would like to use a lower poly option like the Icosphere or Icosahedron.