I want to have a script in my game that adds a card.
I am using a Card class for all my cards, so I don't have to make a prefab for each card. Instead I can do something like: Card fire = new Card("blah blah"); to define a "fire" card type, then call fire.Generate(position) to create a card sprite of that type at the given position.
Somehow I need my Generate() method to add a new object (sprite) to the game when called. How can I do that?
CardGenerator.cs:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CardGenerator : MonoBehaviour { Card makeYellow; private void Start() { makeYellow = new Card("Make Yellow", "This card makes the player yellow."); makeYellow.Generate(Vector2.zero); } } Card.cs:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Card { public string Name; public string Description; private Sprite sprite; private SpriteRenderer sr; public Card(string name, string description) { Name = name; Description = description; } public void Generate(Vector2 pos) { // ... } }