- Home /
MissingReference after removing component from gameObject
Hi guys. When I am trying to remove a component from my gameobject (doing it via script), I got an error. My code:
public class Test: MonoBehaviour
{
private MeshFilter m_Filter;
public void createMesh()
{
if (m_Filter == null)
{
m_Filter = gameObject.AddComponent<MeshFilter>();
}
}
public void removeMesh()
{
if (m_Filter != null)
{
if (Application.isEditor)
DestroyImmediate(m_Filter);
else
Destroy(m_Filter);
}
}
}
and editor script for this:
[CustomEditor(typeof(Test))]
public class TestEditor: Editor
{
new public Test target
{
get
{
return base.target as Test;
}
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Create mesh"))
{
target.createMesh();
}
if (GUILayout.Button("Remove mesh"))
{
target.removeMesh();
}
}
}
and after clicking "remove mesh" button, mesh get removed, but then exception pops:
after I double-click on exception in console, editor shows next window:
Thanks for help in advance.
there's something else in the project referencing the destroyed meshfilter, might be another component (renderer ?)
it's not so easy to find answer for you, if i cant open the project :D but i'd try to find the thing calling meshfilter OR i'd use different way - for example you could just clear mesh m_Filter.mesh.Clear() while you have the old mesh stored elsewhere ... it depends on how often you use the methods etc.
Any luck resolving this issue? I'm having the same problem.
Your answer
Follow this Question
Related Questions
Script to create instance from in inspector 2 Answers
How to record hideFlags for Undo/Redo 0 Answers
How To Force an Inspector to Repaint 3 Answers