In many times, prefabs that share the same script may require the same GameObject in the script. For example, in my game I want to make a series of card prefabs. Each card has their own image when shown, but has the same back image when hidden. My script for card is:
public class DraggingCard : DragAndDropItem
{
public Sprite secretSprite; //This is the same for every card
Sprite oringinalSprite; //This is individual for every card
Image image;
public void Awake()
{
image = GetComponent<Image>();
oringinalSprite = image.sprite;
}
public void Activate()
{
image.sprite = oringinalSprite;
}
public void Hide()
{
image.sprite = secretSprite;
}
}
But if I write this, I'll have to assign the secretSprite
in the inspector for every DraggingCard prefab. I tried to set secretPrefab
to be static
or const
but neither worked.
I believe there must be a clever way to avoid the repeat work. Could anyone tell me that? Thank you!
DraggingCard
are all instances of the same prefab, or if there are multiple base prefabs? \$\endgroup\$ – Gnemlock Jan 21 '17 at 21:53Image
whose sprite isoriginalSprite
, and is attached with aDraggingCard
script. \$\endgroup\$ – DarkZero Jan 22 '17 at 2:50