Instantiating a MonoBehavior Class at runtime and pass it a normal Class

Hi, I’m trying to instantiate a MonoBehavior Class “CardActions” that will have a parameter “Card” which will be a Normal Class.

Card newCard = new Card(cardNumber, cardID, nbCardsInHand);
Instantiate(cardPrefab); //Gameobject prefab that has "CardActions" as component
cardPrefab.GetComponent<CardActions>().setCard(newCard);

The problem with this (I think) is that “CardActions” don’t have the time to set his “default” parameters. So right after the call to setCard(newCard), CardActions sets his parameter Card to

Card card = new Card(); 

Is there any way I should do this? Maybe I’m totally off the track!
Thanks!

Simply check if card already exists before setting it to new.

An alternative is to run the assignment in Awake. This is the typical order that functions will run. By moving something into the right function you can achieve most behaviours.

  • Instantiate called
  • Awake runs
  • The line after Instantiate resumes
  • Start runs

(Might want to run a test to make sure I have the order right, been a while since I’ve needed this).