hello . i am new to unity and trying to create a TCG kind of game.
the problem that i am facing now is to instantiate my prefab and load its asset at single click.
this is my script attached to the card prefab
public class CardData : MonoBehaviour
{
public SOcard socard; //scriptableobject script
public string TitleData;
public Image CardImageNormalData;
public int PowerData;
public int ResistanceData;
public Sprite CardImageLargeData;
void Start()
{
}
public void LoadCardData(SOcard spawncard)
{
socard = spawncard;
TitleData = socard.Title;
CardImageNormalData.sprite = socard.CardImage;
PowerData = socard.Power;
ResistanceData = socard.Resistance;
CardImageLargeData = socard.CardImageLarge;
}
}
next is script attached to deck
public class DeckLogic : MonoBehaviour
{
public List<SOcard> card = new List<SOcard>();
public SOcard socard; //scriptableobject script
public CardData CardTransfer;
void Awake()
{
CardTransfer = FindObjectOfType<CardData>();
socard = card.ElementAt(0);
}
public void addthiscard()
{
var SpawnCard = socard;
CardTransfer.LoadCardData(SpawnCard);
}
and script to my start button.
public class Instantiatetest : MonoBehaviour, IPointerClickHandler
{
private DeckLogic decklogic;
void Start()
{
decklogic = FindObjectOfType<DeckLogic>();
}
public void OnPointerClick(PointerEventData eventData)
{
GameObject card = Instantiate(Resources.Load("Card",
typeof(GameObject))) as GameObject;
loadcard();
}
void loadcard()
{
decklogic.addthiscard();
}
my idea is to click and instantiate card prefab at then transfer the first scriptableobject info from deck to that instantiated card prefab.
when i clicked start button, i got this error;
gameobject reference is not set to an instance of an object.
if i created instance of the card prefab before transferin the info, it works. so i came to conclusion that both this code
`GameObject card = Instantiate(Resources.Load(“Card”, typeof(GameObject))) as GameObject;
and
loadcard();`
are executed at same time.
so now i am trying to find solution to execute the code in sequence.
or maybe i have wrong idea on how this works. im asking for any suggestion.
thanks in advance.