コガネブログ

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

【Unity】You are trying to replace or create a Prefab from the instance 'XXXX' that contains the script 'YYYY', which does not derive from MonoBehaviour. This is not allowed.

概要

f:id:baba_s:20211110091505p:plain

Saving failed. Check the Console window to get more 
insight into what needs to be fixed.

プレハブを編集して保存する時に上記のエラーダイアログが表示されて

You are trying to replace or create a Prefab from the instance 'XXXX' 
that contains the script 'YYYY', which does not derive from MonoBehaviour. 
This is not allowed.
Please change the script to derive from MonoBehaviour or remove it 
from the GameObject.
UnityEditor.EditorApplication:Internal_CallGlobalEventHandler ()

Console ウィンドウにこのようなエラーメッセージが表示される場合は

f:id:baba_s:20211110092622p:plain

プレハブに含まれているゲームオブジェクトに
MonoBehaviour を継承していないクラスのスクリプトがアタッチされている可能性があるため

f:id:baba_s:20211110092650p:plain

MonoBehaviour を継承するように修正するか、

f:id:baba_s:20211110092945p:plain

もしくはそのスクリプトをゲームオブジェクトから Remove Component すれば
プレハブを保存できるようになります