- Home /
How do I access Mesh/ Meshfilter in Editor code?
Hi, i have been having some problems with regards to accesing my mesh in editor mode. Every time i try
GetComponent<MeshFilter / SkindMeshRenderer>().SharedMesh/Mesh;
I get null. when i just try to get the component it also returns null. I instantiate my object in editor mode first
(GameObject)UnityEngine.Object.Instantiate(gameObject);
And the debugger claims it exists so i dont know why its not working.
I also tried
GetComponentInChildren<>();
Weird
Answer by ScroodgeM · Jul 27, 2012 at 02:36 PM
this script adds menu 'UnityAnswers/GetAndAnalizeMesh' and debugs verts and tris count. works fine in editor. works with project view and scene view.
GetAndAnalizeMesh.cs
using UnityEngine; using UnityEditor; using System.Collections; public class MeshAnaliser : MonoBehaviour { [MenuItem("UnityAnswers/GetAndAnalizeMesh")] static void GetAndAnalizeMesh() { GameObject go = Selection.activeGameObject; if (go) { MeshFilter mf = go.GetComponent<MeshFilter>(); if (mf) { Mesh m = mf.sharedMesh; if (!m) { m = mf.mesh; } if (m) { Debug.Log(string.Format("vert:{0} tris:{1}", m.vertexCount, m.triangles.Length / 3)); } } } } }
Answer by SinisterMephisto · Jul 27, 2012 at 03:22 PM
Thanks man. I realized i could not edit meshes of no instantiated objects so i had to create them put them in a separate layer and hid flags for what i wanted. Thanks anyway
Answer by SinisterMephisto · Jul 27, 2012 at 03:22 PM
Hey thanks man. I just realized you had to instantiate the objects to have access to all that. So i had to create and hide it off screen. Thanks for the reply.