Access inspector-created data in MonoBehaviour class from non-MonoBehaviour class

Hi,

I’m trying to make a class that generates a button object with a specific Button prefab stored in a different class, but can’t quite seem to get it to work; I’m getting two errors, “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” and “ArgumentException: The Object you want to instantiate is null.” The following is the code I’m using:

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

public class ButtonHolder : MonoBehaviour
{
    public ButtonData buttonData;
    public GameObject buttonPrefab;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

public class OButton
{
    public delegate void onClickDele();

    public int icon;
    private Sprite iconsprite;
    public string buttonName;
    public onClickDele onClickFunc;

    public OButton(int icon, string buttonName, onClickDele onClickFunc, ButtonData buttonData) {
        this.icon = icon;
        this.buttonName = buttonName;
        this.onClickFunc = onClickFunc;

        this.iconsprite = Sprite.Create(buttonData.textureList[icon], new Rect(0,0,buttonData.textureList[icon].width, buttonData.textureList[icon].height), new Vector2(0.5f, 0.5f));
    }
}

public class QButton
{
    public int icon;
    public string name;
    public Button button;
    public GameObject buttonObject;
    public Action act;

    public QButton(int icon, string name, int x, int y, Action act) {
        this.icon = icon;
        this.name = name;
        this.buttonObject = UnityEngine.Object.Instantiate((new ButtonHolder()).buttonPrefab, new Vector3(x, y, 0), Quaternion.identity);
        this.button = this.buttonObject.GetComponent<Button>();
    }
}

(Edit: Forgot to mention, the “OButton” class was my first attempt, before I realized that it would be better to instantiate a Button instead of a Sprite.)
The following is the code for the “ButtonData” class:

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

public class ButtonData : MonoBehaviour
{
    public List<Texture2D> textureList;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

The following is a screenshot of the data in the inspector:

204784-screenshot-2023-02-15-172101.png
204785-screenshot-2023-02-15-172128.png

How would I go about doing this?

Here this line you use new for a monobehaviour:

this.buttonObject = UnityEngine.Object.Instantiate((new ButtonHolder()).buttonPrefab, new Vector3(x, y, 0), Quaternion.identity);

you can’t create monobehaviour instances using the new keyword because it breaks unity functionality, monobehavious have special things that need to be done in their constructors so they work with the unity engine, this is why there is an Instantiate method. And I’m guess that is causing the 2nd error the new keyword doesn’t work properly making the object you want to instantiate null. This last part is more of a guess, next time include the full error you omitted which lines are triggering the error, which is very useful debugging information.