Hi there,
So I am getting this “You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed.” error.
But I sense, that I should probably not be doing what I was trying to do anyway. ![]()
I am new to Unity and I only used to do a little bit with Java some years ago.
The end goal is a card game, in which the cards are dynamically assembled in a list before any gameObjects are instantiated and distributed in the scene.
So I created a new script defining a card class derived from monobehavior with some attributes, functions and a constructor.
I then attached that script to a 3d model card prefab.
And finally I tried to fill up the list of cards in another ‘higher level’ script.
That is when I came across the error mentioned above.
Here it is in shortened/simplified code:
public class CardObject : MonoBehaviour {
public int card_id;
public int icon_number;
public int color;
public int background_type;
public bool reveal;
Animator my_animator;
GameManager my_game_manager;
// constructor
public CardObject(int new_card_id, int new_icon_number, int new_color, int new_background_type, bool new_reveal) {
card_id = new_card_id;
icon_number = new_icon_number;
color = new_color;
background_type = new_background_type;
reveal = new_reveal;
}
// ----------------------------------------------------------------------------
private void Awake() {
my_animator = GetComponent<Animator>();
reveal = false;
my_animator.SetBool("reveal", reveal);
my_game_manager = GetComponentInParent<GameManager>();
}
// ----------------------------------------------------------------------------
private void OnMouseDown() {
//...
}
public void FlipTo(bool showFace) {
//...
}
//and so on...
}
public class GameCreator : MonoBehaviour {
// ...
List<CardObject> cards;
// ...
// ----------------------------------------------------------------------------
void Start () {
//...
cards = new List<CardObject>();
//...
cards.Add(new CardObject(cards.Count, 1, 2, 0, false));
//...
}
Now, I don’t know if I even have the right approach to my whole structure.
I realize, I should not be creating instances that are not yet linked to any gameObjects. And I also just read, that should not use a constructor in a monobehavior class anyway.
So what is the best practice here.
Should I maybe separate the card class from the script attached to the 3d object, so that it do not need monobehavior?
Or, should turn around the order of things and initialize all cards as ‘general purpose’ objects in the scene and only later manipulate them by adding or deactivating components, changing attributes and so on?
And how does adding a new card item into the list actually look in the end (without a constructor and whatnot)?
Thanks in advance.