Trying to use MonoBehaviour new, not working

Alright so I have a simple “store” UI that pops up when you hit a key. When you buy something by clicking one of the UI buttons, I want it to add that item to my list of items in my inventory script.

Here is the inventory script:

using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour {
    [SerializeField]
    private invItem[] invItems = new invItem[10];

    public void addInvItem(int index, invItem inItem)
    {
        for(int i = 0; i <= invItems.Length; i++)
        {
            if (i == index)
            {
                invItems[index] = inItem;
            }
        }
    }
}

Here is the invItems that it is referencing:

public class invItem : MonoBehaviour {
    public string itemName;
    public int cost;

    public invItem(string inName, int inCost){
        itemName = inName;
        cost = inCost;
    }
}

and here is the button script:

    [SerializeField]
    private GameObject GameControl;
    void Start()
    {

    }
    public void addBasicTurret()
    {
        invItem basicTurret = new invItem("Basic Turret", 5);
        GameControl.GetComponent<Inventory>().addInvItem(0, basicTurret);
    }

I don’t get any errors, but when I try and use the button in order to add an invItem object into my array, I get this warning:

Any idea what I’m doing wrong here? I kinda understand that the warning is trying to tell me not to use “new” but I’m not sure how else to do it.

It gives you alternatives right in the error message:

public class invItem {

If you aren’t ever attaching it as a component to a GameObject.

Problem is, when I do this, my array doesn’t show up in the inspector, which is causing me a lot of hassle. I tried this originally but it wouldn’t work because of that.

Is there any way I can take out monobehaviour and keep it in the inspector?

Add [System.Serializable] just before the declaration of the class.

Alternately, you could leave it as a MonoBehaviour and attach it as a script, but then use gameObject.AddComponent as the warning suggested to.

Wow, that worked perfectly! Thanks a bunch.