Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DimensionXYZ · Dec 23, 2014 at 06:52 PM · c#gameobjectgridgrid based game

How do I create roadblocks in my Match 3 puzzle gameboard?

Some background info on me and what I’m doing before I get to the problem. I would consider myself somewhere in between beginner and intermediate level programmer as I understand every programming concept but have problems when putting it into practice. I’m in the process of making a Match 3 puzzle game. I followed some tutorials and have an actual working game (with some kinks).

On to the problem:

I used a nested for loop to instantiate a grid with GameObjects (gems).

 for (int y=0; y<GridHeight; y++) {
     for (int x=0; x<GridWidth; x++) {
     
         // Instantiate the GameObject with prefabs
         GameObject g = Instantiate (gemPrefab, new Vector3(x,y,0), Quaternion.identity) as GameObject;
         g.transform.parent = gameObject.transform;
         Gems.Add (g.GetComponent<gem>());
     }
 }

The GridHeight & GridWidth are public variables so that I could have grids in varying sizes. The gemPrefab consists of a random list of materials that changes the gem different colors). The problem is that this code produces an endless list of random gems which I do not want. I want to be able to add puzzle elements to the grid such as making holes/blocks within the grid that block the gems from falling. I am completely stuck on how to do this.

One way I thought of possibly doing this is by making a 2D array with certain indexes instantiated with my “Blocks”, then having a nested for loop to instantiate the null/empty indexes with my gems.

 GameObject g = Instantiate (blockPrefab, new Vector3 (1, 3, 0), Quaternion.identity) as GameObject;
         g.transform.parent = gameObject.transform;
         gems.Add(g.GetComponent<Gem>());

However, when I instantiate my 2D array’s index with my blocks and then do the nested for loops, it does not instantiate the rest of the loop and I get the one block GameObject floating on the grid.

In a sense, I could instantiate my original “grid” to include my blocks, but that would place them at random places (defeats the purpose of creating a puzzle mechanic). I've looked into the many different frameworks available on the asset store but I much rather program it myself (with some help of course). Could someone shine a little light on what I’m doing wrong?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Dec 23, 2014 at 07:15 PM

The standard way to make a grid game is to have a "backing" 2D array. Then to think of the stuff the player sees as merely a picture of it. Old idea, and you can probably find how to work with 2D game grids in much better places than Unity. Unity is only at the "now show the player" step.

Ex (illegal code, to shorten it):

 Transform[,] GridTs = new Transform[10,10];
 // hold the actual items (holes, gems...at each pos)

 int[,] Grid = new int[10,10];
 // code for what's there
 // 0=nothing yet, -1=obstacle; 1-8=gem of that color
 // or use a class, or enum ... but integers work just fine.

The hole-making code can place -1's where-ever. Then the gem-making code checks Grid[x,y] and skips -1. The key is, whenever you want to know what's in a space, don't look in the gameWorld. Check Grid[x,y].

Last step is to go through the created grid and Instantiate what each numbers says to.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by realclues · Oct 25, 2016 at 05:34 AM

DimensionXYZ how do you apply the given code by Owen-Reynolds in your project? I used the same code as you.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How to only show objects in range ? 2 Answers

How to debug the object position in the grid of objects? 0 Answers

Distribute terrain in zones 3 Answers

Creating an Object with a string C#! How? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges