- Home /
Instantiating a prefab that has script attached
Hey, I'm new and a bit confused so please have some patience.
So the context is:
I have a "Card" prefab(made out of 2 planes that represent the front and the back of my card). The prefab has two scripts attached to it: "Card" and "CardData".
public class Card : MonoBehaviour {
public string cardName;
public int cardManaCost;
public int cardHp;
public int cardAttack;
}
I have a function that is supposed to instantiate a prefab and give it the correct texture that coresponds with the values from the "Card" and "CardData" scripts.
My question is:
When I instantiate the prefab, does it automaticly create an object of the class in the script? If yes, can I access it like this?
newCardName = clonedPrefab.Card.cardName;
Do I need to create a "Card" object myself? How does this work.
Any help would be greatly appreciated, I'm in a stage where it's really hard to test things in my project and I can't find this info in the documentation so I'm kind of stuck.
Answer by Seth-Bergman · Sep 17, 2013 at 08:22 PM
When you instantiate your object, you are also instantiating an instance of each component attached to it.. to access the component, you can use GetComponent:
newCardName = clonedPrefab.GetComponent<Card>().cardName;
Your answer
![](https://koobas.hobune.stream/wayback/20220613114348im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
instantiating vertically 2 Answers
Instantiation in C# 1 Answer
Instantiate already connected players. 0 Answers