Removing a Component that is a Nested Class.
I'm having an issue with nested classes that extend MonoBehaviour, the code attached is a boiled-down version of what we are doing.
When you call "tapButton" GameObject.Destroy is NOT fully destroying the object, if you run the profiler and look in scene memory, another instance of "MonoScript" is added, and that will never go away, even if you destroy the game object, it is permanently reference by "MonoManager".
using UnityEngine;
public class SharedProfileTesting : MonoBehaviour
{
private StateA _state;
public void tapButton()
{
if (_state != null)
{
GameObject.Destroy(_state);
}
gameObject.AddComponent<StateA>();
}
public class StateA : MonoBehaviour
{
}
}
Is there any way to properly destroy a component that is an inner class? For the record, if StateA is not an inner class, the issue does not occur.
I would first like to say, my gut instinct is that having a nested class that is a component seems like a bad idea. I'm surprised unity let you add such a component to a game object int he first place.
Your component scripts should be in a file whose filename matches the class name. It has to do with the way Unity compiles scripts.
It's not an answer to your question, because your question shouldn't exist. Just don't make StateA a nested class. Put it in a separate file.
You are not storing the reference int the variable making _state always null.
Use:
_state=gameObject.AddComponent<StateA>();