Using a non-monobehaviour class file

I’m working on a trading card game. This is my cardData class (simplified):

public class cardData : MonoBehaviour {
	
	public string cardName;
	public int cardManaCost;
	public int cardHp;
	public int cardAttack;
	
	
}

I have a function that creates a card by cloning the card prefab that contains the above script.

Card createCard(int n){
  clone the prefab;
  pick the data for the n-th card in a List that contains the data for all the cards;
  assign the right texture for the card;
  return the card;
}

So as you can see at step 2 in the “createCard()” function, I want to have a list with the data for all the cards. So I created another class “cardData2” that is the same as “cardData” displayed above ^. I want to create objects of this class and put them in a List.

My question is: how do I create instances of this class? I can’t work with Monobehaviour because I don’t want to attach this script to any game object. Or do I?

I’m confused, and I’m looking for the best solution here but also looking to understand how Unity works with non-monobehaviour classes. I can’t seem to instantiate this script if it’s just in my resources folder. Please let me know what would be the best approach in your oppinion.

Thanks

As the MonoBehaviour-name implies, they are behaviors, not data classes. If your CardData (classes are normally capitalized in C#) does not actually have any impact on the GameObject or the game world at large, then it is better to not extend MonoBehaviour.

You can work with non-MonoBehaviour classes in Unity as you would in any software development: normal classes deriving from System.Object. The only difference to a MonoBehaviour class is that you cannot add them to GameObjects as they are not Components.

You should never be in a position where you absolutely need to have two exactly same classes, differing only in one variable or a few lines of code. If you sometimes do find this is the case, then there is something wrong with your design and you need to sit down and think where the problem is.

To answer your (only answerable) question, you create instances of non-MonoBehaviour classes like you do any normal class, with the new-keyword. The other part of your question is a design decision that only you can answer: Does the class serve any purpose as a behavior on an object in your game? If the answer is in the negative, then don’t add it, if on the affirmative, then add it.

Best Solution : Unity - Manual: Running Editor Script Code on Launch