Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Matching Card Game - decorating cards issue

Discussion in 'Scripting' started by LT-Dev, Mar 13, 2023.

  1. LT-Dev

    LT-Dev

    Joined:
    Feb 27, 2019
    Posts:
    1
    Hello. I'm working on a little matching game (C#) where the player have to match two cards.
    There are 8 cards on the desk and the player has to find two matching ones.

    I have made one prefab called "card" - the card gets the texture (front and back) from a scriptable object. I have four scriptable objects called Star, Apple, Heart and Banana.

    So far I have managed to place 8 cards on the screen all with the "back texture" showing - and when clicked, it shows the front side (either star, apple, heart or banana).

    But I have issues decorating the cards - I know I have to select two cards, then inject it with one of the scriptable objects; and then head on to the next two - decorate them and so on.

    But I just cant figure out how to do so..

    Am I overcomplicating a simple game by this approach? Would it be better to make a prefab for card (star, apple, banana, heart) and then just load two of each card into the gamescene?

    Any input appreciated!
     
  2. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    960
    In general, I'd say yes you are definitely over complicating it ;) Using multiple prefabs would work just fine. In fact, you can even make one prefab and then several prefab variants based off of it, each with their own image.

    On the other hand if you are specifically trying to learn a technique like how to dynamically apply textures to an object then you'll have to give us a little more to help you work off of. What part are you struggling with specifically?
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,646
    Why don't you just 'inject' the scriptableObject data at the start? Then when the card is 'flipped', you show the cached texture you gave it from the beginning.
     
  4. SeerSucker69

    SeerSucker69

    Joined:
    Mar 6, 2021
    Posts:
    68
    I wouldn't use scriptable objects, you can just change the sprites on the game objects

    Code (CSharp):
    1. myrend.sprite = Resources.Load<Sprite>("Things/" + mything.Spritename);
    Use an Array to hold card data, assign them all their values (2 of each) and "shuffle" them into random positions. When the player selects a card, you look up the card type in the array, then change the sprite to display it.