Hello. I have the next code (simplified)
public class MapComponent : MonoBehaviour
{
/* Many other fields - settings*/
public GameObject tilePivot;
void OnValidate()
{
if (Application.isPlaying) return;
else if( /* Settings changed */)Regenerate();
}
private void Regenerate()
{
if (tilePivot != null) {
DestroyImmediate(tilePivot);
}
tilePivot = new GameObject("Map");
}
When im spawn object in editor, it use regenerate() method to make himself and spawn child objects.
Child objects saving as childrens of a “tilePivot”.
On some parameter changes, script use tilePivot to remove all child objects, and spawn they again.
In editor everything work nice.
When im enter to play mode, “tilePivot” value becomes null, but child objects still exists, and at this step everything wok as expected, because object dosent need changes in playmode.
But after exit the play mode, tilePivot is still null and im getting a duplicated objects at next OnValidate event.