コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)

概要

using UnityEngine;

public class Example : MonoBehaviour
{
    private void OnDestroy()
    {
        new GameObject();
    }
}
using UnityEngine;

public class Example : MonoBehaviour
{
    public GameObject m_prefab;

    private void OnDestroy()
    {
        Instantiate( m_prefab );
    }
}

上記のように OnDestroy で new GameObject()Instantiate( m_prefab )
ゲームオブジェクトを生成していると

Some objects were not cleaned up when closing the scene. 
(Did you spawn new GameObjects from OnDestroy?)
The following scene GameObjects were found:
XXXX

上記のエラーが発生します