Instance of a class, adding it to a List

Hello !

I’m new to unity, but not that new to C# and I want to ask a really average and regular question which I don’t know how to search after.
I made a class called ConsumableItem.cs.

using UnityEngine;
using System.Collections;

public class ConsumableItem : MonoBehaviour {

	public string _itemName;
	public short _manaPlus;
	public short _healthPlus;
	
	public ConsumableItem (string itemName) {
		_itemName = itemName;
		
		if (_itemName == "Gyógyital") {
			_manaPlus = 0;
			_healthPlus = 40;
		}
	}
	
	public short HealthPlus
	{
		get
		{
			return _healthPlus;
		}
	}
	
	public short ManaPlus
	{
		get
		{
			return _manaPlus;
		}
	}
}

What I want to do is, I want to add an instance of the ConsumableItem to my Globals.cs class.
My Globals.cs class looks like this:

using UnityEngine;
using System.Collections;

public class Globals : MonoBehaviour {
	public static byte partyNumber = 2;
	public static ArrayList Items = new ArrayList();
	public static byte itemNumber = 5;
}

What I want to do would look like this in C#:

ConsumableItem a = new ConsumableItem ("Gyógyital");
Globals.Items.Add (a);

Of Course I got the error, which I’ve read about at several places

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

So my question is, what’s the proper method for doing anything like this in unity C#?

Thanks in advance !

You have two choices. The best choice given your information here is to not derive your ConsumableItem from MonoBehaviour. I don’t see anything in the class that benefits from deriving it from MonoBehaviour, but this may not be the entire class. If you don’t derive from MonoBehaviour, the it is just a standard C# class. You can use constructors and can create instances of the class using the ‘new’ operator.

Your second choice is to create your object as a component. You would do something like this:

GameObject go = new GameObject();
ConsumableItem a = go.AddComponent<ConsumableItem>();
a.SetItem("Gyógyital");
Globals.Items.Add(a);

Where ‘SetItem()’ is a new method that does what your constructor did in the original class.