- Home /
Do gameobjects not have a mesh component by default?
I'm trying to create a custom mesh from code and instantiate it in the game. Unity is telling me I'm trying to access a component that isn't present on my object. If Mesh is not in fact a default component of gameobject, what is the best thing to be doing here?
Mesh mesh = new Mesh();
GameObject shape = new GameObject ("Shape");
shape.GetComponent<MeshFilter>().mesh = mesh;
Then after this I'm setting all of the mesh properties.
by 'custom mesh' I mean one I'm creating procedurally from code. I guess another part to this question is - what exactly is a meshfilter and why isn't there one inherently in a gameobject? I thought go's had transform data, mesh renderer componenets etc. (sorry for the newbie question!)
No, the only component that all game objects have is a transform (you wouldn't want a camera or a light to have a mesh renderer, would you?)
Answer by tanoshimi · Jun 17, 2014 at 12:41 PM
shape.AddComponent<MeshFilter>();
http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html
Answer by bobblehat · Jun 17, 2014 at 12:42 PM
By custom mesh what exactly do you mean? If you want to load a mesh (that exists as an asset) -- maybe making a "Prefab" is good enough. Prefabs are GameObjects that you have already attached scripts/renderers/other components to, that can then be instantiated in one go from code.
Your answer
Follow this Question
Related Questions
Turning off renderer in other game object (C#) 2 Answers
C# Preserving GameObjects' Previous Meshes 1 Answer
Move gameobject pivot 6 Answers
Attaching a model to a blank GameObject? 0 Answers
C# Array of OtherArray's Meshes 1 Answer