UnityException: ScriptableObject.ctor is not allowed to be called from a ScriptableObject

someone help me with the code. what wrong with the code i just uprage my prject and the editor wont work.

UnityException: ScriptableObject.ctor is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject 'ServerPlugin'.
See "Script Serialization" page in the Unity Manual for further details.
UnityEngine.ScriptableObject..ctor () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/BaseClassBindings.gen.cs:461)
TestFunction..ctor () (at Assets/TestUnity/Editor/TestEditor/Function.cs:19)
ServerDataBase..ctor () (at Assets/Test/TestavismUnity/Editor/Plugins/ServerPlugin.cs:24)
UnityEngine.ScriptableObject:CreateInstance(String)
TestUnity:Installplugins() (at Assets/TestUnity/Editor/TestEditor/TestUnity.cs:954)
TestUnity:OnGUI() (at Assets/TestUnity/Editor/TestEditor/TestUnity.cs:764)
UnityEditor.DockArea:OnGUI()

Here’s the full code

using System;
using System.Collections.Generic;
using UnityEngine;

public class TestFunction : ScriptableObject
{
    public string functionName;

    public string breadCrumb = "";

    public Texture functionIcon;

    public string functionCategory;

    public Help help;

    private string newItem = "";

    [B]private string newText = "";  (Im getting error with this)[/B]

    private bool editMode;

    public virtual void Activate()
    {
    }

    public virtual void Draw(Rect box)
    {
        GUI.Label(box, "Select a Plugin and Function to Edit");
    }

    public virtual float DrawHelp(Rect box)
    {
        if (this.help == null)
        {
            this.help = new Help(this.functionName);
            this.help.Load();
        }
        Rect rect = box;
        rect.x = (rect.x + ImagePack.innerMargin);
        rect.y = (rect.y + ImagePack.innerMargin);
        rect.width = (rect.width - ImagePack.innerMargin);
        rect.height = (ImagePack.fieldHeight);
        rect.y = (rect.y + ImagePack.fieldHeight);
        ImagePack.DrawLabel(rect.x, rect.y, this.functionName + " Editor Help");
        rect.y = (rect.y + ImagePack.fieldHeight);
        List<HelpEntry> list = this.help.Get();
        for (int i = 0; i < list.Count; i++)
        {
            if (this.editMode)
            {
                Rect position = rect;
                position.width = (position.width * 0.2f);
                list[i].Item = ImagePack.DrawField(position, list[i].Item);
                position.x = (position.x + position.width);
                position.width = (position.width * 4f);
                list[i].Text = ImagePack.DrawField(position, list[i].Text);
                Rect position2 = new Rect(rect.x + rect.width - 20f, rect.y, 20f, rect.height);
                if (ImagePack.DrawSmallButton(position2, "X"))
                {
                    list.Remove(list[i]);
                    this.help.Save();
                }
                rect.y = (rect.y + ImagePack.fieldHeight);
            }
            else
            {
                int num = ImagePack.DrawMultilineText(rect, list[i].Item + ": " + list[i].Text);
                if (num < 2)
                {
                    rect.y = (rect.y + ImagePack.fieldHeight * 1.25f);
                }
                else
                {
                    rect.y = (rect.y + (ImagePack.fieldHeight + ImagePack.fieldHeight * 0.85f));
                }
            }
        }
        rect.y = (rect.y + ImagePack.fieldHeight);
        if (this.editMode)
        {
            Rect position3 = rect;
            position3.width = (position3.width / 2f);
            if (ImagePack.DrawButton(position3, "Save and Exit"))
            {
                this.help.Save();
                this.editMode = false;
            }
            position3.x = (position3.x + position3.width);
            if (ImagePack.DrawButton(position3, "Exit without Save"))
            {
                this.help.Load();
                this.editMode = false;
            }
            rect.y = (rect.y + ImagePack.fieldHeight);
            ImagePack.DrawLabel(rect, "New Help line:");
            rect.y = (rect.y + ImagePack.fieldHeight);
            rect.width = (rect.width / 2f);
            this.newItem = ImagePack.DrawField(rect, "Item:", this.newItem, 0.6f);
            rect.width = (rect.width * 2f);
            rect.y = (rect.y + ImagePack.fieldHeight);
            this.newText = ImagePack.DrawField(rect, "Text:", this.newText, 0.8f, ImagePack.fieldHeight * 3f);
            rect.y = (rect.y + ImagePack.fieldHeight * 3f);
            if (ImagePack.DrawButton(rect.x, rect.y, "Save Line") && this.newItem != "")
            {
                this.help.Add(this.newItem, this.newText);
                this.help.Save();
                this.newItem = "";
                this.newText = "";
            }
        }
        else if (ImagePack.DrawButton(rect.x, rect.y, "Enter Edit Mode"))
        {
            this.editMode = true;
        }
        return rect.y - box.y + ImagePack.fieldHeight;
    }
}
1 Like

How are you instantiating TestFunction?
ScriptableObjects require CreateInstance, you can’t instantiate using New.

1 Like

Somehow in that stack chain you are tying to do something in the constructor. It could be:

  • Using a constructor directly
  • Calling new
  • Assigning a complex value when initialising a variable
1 Like

I know this is an old thread, but i had the same thing. I had a scriptableobject script called “SkillChange”, and in another script a made a variable for it like this:

public SkillChange Skill = new SkillChange();

The problem is that I made a “new” scriptable object, and this is not the way to do it.
So check you code if you did something similar (like, in your case: public TestFunction blabla = new Testfunction():wink:
then you know that that’s where your mistake is :slight_smile:

Correct. You must NEVER make Unity native engine objects (such as ScriptableObject or MonoBehaviour) via the new keyword.

To make such native engine objects, the ONLY valid way at runtime is:

For ScriptableObjects:

ScriptableObject.CreateInstance<T>();

For MonoBehaviours:

GameObject.AddComponent<T>();

1 Like