- Home /
Instantiating a Game Object and using Scriptable Objects
Hi, im a beginner and new to C# programming and i don't understand the language very well. I'm working on a card project like hearthstone but in 2D. I'm having issues in Instantiating a GameObject (which is a prefab of a empty card) and then give the card his information (scriptable Object). I tried already but im having difficulties because i have no idea on how to instantiate and put the concepts together. Can someone give me an example on how to do it? Right now i created a list of scriptable objects inside of the players deck. Now i have to instantiate the empty card (GameObject) and then attribute the card a random Scriptable Object, so when the player clicks on the deck it gives you the "full" card with the attack and etc...
This is what i have now:
public class DrawPlayerCard : MonoBehaviour { public List cards; // PlayerCards are the scriptable Objects public GameObject playerHand; // This is where the cards are going to stay when drawn
List<PlayerCards> cardsHand;
// Start is called before the first frame update
void Start()
{
cardsHand = new List<PlayerCards>();
}
// Update is called once per frame
void Update()
{
}
}
// The GameObject that i need to Instantiate has a script named Card.
Answer by Hellium · Sep 23, 2020 at 04:24 PM
public class DrawPlayerCard : MonoBehaviour
{
public List<PlayerCards> cards; // PlayerCards are the scriptable Objects
public Card playerHand; // prefab of the card
List<PlayerCards> cardsHand;
// Start is called before the first frame update
void Start()
{
InstantiateCards();
}
void InstantiateCards()
{
cardsHand = new List<PlayerCards>();
for(int i = 0 ; i < cards.Count ; ++i)
{
Card card = Instantiate(playerHand);
card.Initialise(cards[i]);
}
}
}
// In your Card class
{
public void Initialise(PlayerCards playerCards)
{
// Do something with the playerCards scriptableObject
}
}