Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to Instantiate an array of prefabs (Sprite Images)

Discussion in '2D' started by kystro, Apr 24, 2022.

  1. kystro

    kystro

    Joined:
    Mar 8, 2022
    Posts:
    2
    Hi Guys, I started to learn C# (new to code) in Unity and I'm trying to build a slot machine game with a 5x3 grid.

    I'm first trying to figure out how to instantiate the symbols (200x200px sprite image) onto the 5x3 grid in random order.

    Right now, I have the elements declared. Not sure how to go about instantiating each symbol within an array, adjacent to each other, both x and y axis.

    Can anyone help me how to go about with this, please? Thank you.

    Apologies for the newbie question.


    Code (CSharp):
    1. public class Script : MonoBehaviour
    2. {
    3.  
    4.     //Number Grid > Configured in the Inspector
    5.     public int numOfRow;
    6.     public int numOfCol;
    7.  
    8.     //Size of Grid
    9.     double gridWidth;
    10.     double gridHeight;
    11.  
    12.     //size of each symbol
    13.     double symbolWidth;
    14.     double symbolHeight;
    15.  
    16.     //Array of symbols > Prefabs imported in the Inspector
    17.     public GameObject[] symbols;
    18.  
    19.     void Start()
    20.     {
    21.         GenerateGrid();
    22.     }
    23.  
    24.     void GenerateGrid()
    25.     {
    26.  
    27.         for (int col = 0; col < numOfCol; col++)
    28.         {
    29.             for (int row = 0; row < numOfRow; row++)
    30.             {
    31.              
    32.             }
    33.         }
    34.  
    35.     }
     
    Last edited: Apr 24, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,948
    No problem at all, but as a professional who has been paid to make actual slot machines, let me urge you to go find some tutorials on the basics of making a slot machine. It's quite a well-understood sequence that must happen.

    The reason is that by spawning random prefabs at this stage you are only setting yourself up for a VERY hard time actually evaluating paylines after each spin.

    The flow needs to be something like:

    - develop a data structure that represents the state of the reels (contents of the window)

    - generate a random result in that data structure

    - use that data structure to:
    --> generate the tiles in the grid
    --> evaluate the payout.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
    kystro likes this.
  3. kystro

    kystro

    Joined:
    Mar 8, 2022
    Posts:
    2
    Thanks @Kurt-Dekker

    Really appreciate your advice and you're completely right with the tutorials. My mistake is that I watch tutorials, but never really re-iterate on what I supposedly learnt. I then tend to forget what I leant and become frustrated when I attempt to do my own work.

    I needed this!

    Again, really appreciate you taking your time for this. Have a lovely day!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,948
    One great antidote to that is to break off smaller chunks.

    For instance, try make something that chooses 3 symbols randomly, perhaps from the symbols A, B, C, D, E, F and G.

    Once you have chosen an array of 3 symbols, display them (perhaps with prefabs, or just in Debug.Log()) and then decide if they are winning, according to some made-up table, such as:

    AAA - win 100
    BBB - win 50
    CCC - win 10
    ?AA - win 2
    ?BB - win 1

    etc

    Make it so when you press ENTER it takes away a coin (your bet) and then does the above and gives you coins back according to the table.

    One hundred percent of what you learn in that exercise can be used for a real slot machine. If you master being able to do the above, then set it all aside and try it again a day later, you will be well on your way to understanding this particular problem space. Plus as you go along you'll have a little "micro" slot machine.