Ploblem with the Save Option

Hello! I’m making a 2D project, but when i hit the save button it says me this:
“You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
LoadingScreen:.ctor()
SaveLoad:Load() (at Assets/scripts/SaveLoad.cs:41)
pause:OnGUI() (at Assets/scripts/pause.cs:41)”

Does anyone knows what is my problem?

Sounds to me like some code that runs when you hit the save button (namely, SaveLoad.Load) is trying to create a MonoBehaviour using the ‘new’ keyword.

This is not allowed. (See the error message for more explanation.) So, Don’t Do That.

Double-click the error message, and it should take you right to the offending line of code.

I already tried to click on the warning message, but nothing appens!

You have to double-click. And if it still doesn’t take you there, then just navigate there manually — it’s line 41 of SaveLoad.cs (says so right in the error message).

1 Like

I commentaded that line but it still doesn’t work, when i try to save it says the same thing!

If you have a broken part in your car, you can’t just remove that part and expect your car to work. Code is the same way.

You’ll need to actually rethink what you’re doing here. As the error message explains, you can’t instantiate MonoBehaviours with new. You need to do something completely different; use AddComponent instead, or clone a prefab, or something like that.

So the problem isn’t with that line of code; it’s with the whole approach that code is trying to use.

I’m assuming you wrote this code… you’ll need to rewrite it, with your newfound understanding of how MonoBehaviours work.