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 SlyyGuyy · Feb 10, 2014 at 07:25 AM · c#blocksmatching

How to destroy blocks that are matching in color C#

I'm currently working on a game that is similar to Pet Rescue saga and I was having trouble with figuring out how to store the color variable of the block and find colors of blocks that are the same as that color and then "destroy" that group of blocks. Hopefully someone understands what I'm attempting to ask. I'm struggling to describe what I want accurately, but I will post the code and note the lines where I am running into issues. There is prototyped code within the code at the moment, but please ignore it. In the function "public void DestroyBlocks(Vector2 pos)" I'm attempting to store the color of the block and have it find blocks that are on the top, bottom, left, or right of that block that are the same in color and when the user clicks on one of those blocks, it destroys that group. Also note that "Block.tex" is referencing another CS script. I hope this isn't too complicated/hope I explained it as much as possible so that someone is able to assist me in this. Thank you in advance for your help.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class LuckyLandingGame : MonoBehaviour {
     [System.Serializable]
     public class Board {
         
         public GameObject[,] grid = new GameObject[10,10];
         
         public Vector2 GetBlockPos(GameObject clickedBlock) {
             for (int x = 0; x < 10; x++) {
                 for (int y = 0; y < 10; y++) {
                     if (clickedBlock.Equals(grid[x,y])){
                         Debug.LogError("Found Block at pos: " +x+ ", " +y);    
                         return new Vector2(x*1f,y*1f);
                     }
                 }
             }
         }
         
         public void DestroyBlocks(Vector2 pos) {
             //temp variable stores color
             Vector2 temp = gameBoard.grid[x,z].GetComponent<Block>().tex;
             //change color to blank color or destroy element
             
         }
         
     }
     
     //var bookNerd
     //var encyclopediaBlock
     //var keyOfKnowledge
     
     //int confettiPoints
     public Board gameBoard;
     
     public GameObject block1;
     public Texture blockTexture;
     public List<Color> texBlock = new List<Color>(3);
     public List<Block> blockTypes = new List<Block>();
     int gridWidth = 10;
     int gridHeight = 10;
     //Random rndGrid = new Random();
     
     
     // Use this for initialization
     void Start () {
         
         //randomize colors of blocks (for loop)
         //put player character at the top of stack
         //if user hovers over group of blocks, highlight group
         
         createWorld ();
         
     
     }
     
     // Update is called once per frame
     void Update () {
         
         if(Input.GetMouseButtonDown(0)) {
             Ray blockRay = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(blockRay, out hit)) {
                 if (hit.transform.gameObject.GetComponents<Block>().Length>0) {
                     hit.transform.gameObject.GetComponent<Block>().ClickBlock();
                     gameBoard.GetBlockPos(hit.transform.gameObject);
                     Destroy (hit.transform.gameObject);
                 }
             }
         }
         //if user clicks on 2 or more blocks - destroy blocks
             //if 2 blocks match +4 points
             //if 3 blocks match +9 points
             //if 4 blocks match +16 points
             //if 5 blocks match +25 points
             //if 6 blocks match +36 points
             //if 7 blocks match +49 points
             //if 8 blocks match +64 points
         
         //if user clicks on "pause button" pause game
         //if user clicks on "resume button" unpause game
         
     
     }
     
     void powerUps () {
         //bookNerd powerup
             //enables player to eliminate entire HORIZONTAL row of blocks
             //+4 confetti points for every block
         
         //encylopediaBlock powerup
             //cannot move unless "unlocked"
             //if player has key of knowledge, unlock encyclopediaBlock
         
         //keyOfKnowledge powerup
             //allow player to "unlock" encyclopediaBlock
     }
     
     void createWorld () {
         
         
         //Create a grid of blocks that chooses a random number and assigns an asset to that number
         for (int x = 0; x<gridWidth; x++) {
             
             for (int z = 0; z<gridHeight; z++) {
                 
                 int chooseBlock = Random.Range(0, 3);
                 
                 gameBoard.grid[x,z] = Instantiate(block1, transform.position, transform.rotation) as GameObject;
                 gameBoard.grid[x,z].AddComponent<Block>();
                 gameBoard.grid[x,z].GetComponent<Block>().tex = texBlock[chooseBlock];
                 gameBoard.grid[x,z].transform.parent = transform;
                 gameBoard.grid[x,z].transform.localPosition = new Vector3 (x-4.5f, 1, z-4.5f);
                 gameBoard.grid[x,z].renderer.material.mainTexture = blockTexture;
                 gameBoard.grid[x,z].transform.localScale = new Vector3 (.95f, .95f, .95f);
             }
         }
     }
 }
 
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
3

Answer by barjed · Feb 10, 2014 at 08:01 AM

Please try to describe your issue a bit more because right now I am not sure what is exactly the problem.

If you need a way to destroy all blocks of the same color that are neighbours of each other then it shouldn't be too difficult.

Create a collection for storing temporary list of blocks that will be destroyed. Start from the block that was clicked and iterate through all of it's neighbours (Up, Right, Down, Left). If colors match, then add the neighbour to the collection. If it doesn't, then stop. Now do the same for neighbours of each block that matched the original one. Finally destroy all the blocks that are in the collection.

Here's a really rough example:

 // this list will contain all matched blocks
 private List<Block> matchingBlocks = new List<Block>();
 
 // this variable stores the color we're matching to
 private Color matchingColor;
 
 void CheckBlock(Block block)
 {
     if (matchingColor != null)
     {
         // if the color of the current block matches the matching color
         if(matchingColor == block.Color)
         {
             matchingBlocks.Add(block);
             
             // let's assume that every block object has a list or an array of all it's neighbours called neighbours
             foreach(Block neighbour in block.neighbours)
             {
                 // Recursion
                 CheckBlock(neighbour)
             }
         }
     }
     else
     {
         // if the color we're matching to doesn't exist (it's the original block), then assign the current one to it
         matchingColor = block.Color;
     }
 }
 
 void DestroyMatchingBlocks()
 {
     foreach(Block block in matchingBlocks)
     {
         Destroy(block);
     }
     
     matchingBlocks.Clear();
     matchingColor = null;
 }

Now you simple need to call the CheckBlock method on a block clicked by the user and right after that call DestroyMatchingBlocks.

Comment
Add comment · Show 2 · 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 SlyyGuyy · Feb 10, 2014 at 03:48 PM 0
Share

Thanks for the help, you pretty much nailed what I was attempting to ask. What I'm also wondering is how I would actually go about checking for an object's neighbor because unfortunately there's no easy way to say "hey find the object left, right, up, and down!" in code haha.

avatar image karthickoxygen SlyyGuyy · Jun 02, 2016 at 03:06 PM 0
Share

I use RayCasting for a similar purpose.

avatar image
0

Answer by sitansh · Sep 01, 2017 at 09:59 AM

Please send me the full code Email: sitanshyadav@gmail.com

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding an object's neighbor C# 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Change Block Position In Block Matching Game C# 2 Answers

Shifting blocks to the right to remove empty spaces - C# 0 Answers


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