How Delete Certain Color Blocks In Unity
- Spaces
- Default
- Help Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
0
How to destroy blocks that are matching in color C#
I'g 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 aforementioned as that color and then "destroy" that group of blocks. Hopefully someone understands what I'm attempting to ask. I'm struggling to depict 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 delight ignore it. In the part "public void DestroyBlocks(Vector2 pos)" I'yard attempting to store the color of the cake and have it detect blocks that are on the top, lesser, 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 promise this isn't also complicated/hope I explained it as much as possible and then that someone is able to help me in this. Give thanks you in accelerate for your assistance.
using UnityEngine; using System.Collections; using System.Collections.Generic; public class LuckyLandingGame : MonoBehaviour { [System.Serializable] public class Board { public GameObject[,] filigree = new GameObject[10,10]; public Vector2 GetBlockPos(GameObject clickedBlock) { for (int x = 0; 10 < 10; x++) { for (int y = 0; y < 10; y++) { if (clickedBlock.Equals(grid[10,y])){ Debug.LogError("Found Block at pos: " +10+ ", " +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 colour to blank colour or destroy element } } //var bookNerd //var encyclopediaBlock //var keyOfKnowledge //int confettiPoints public Lath gameBoard; public GameObject block1; public Texture blockTexture; public Listing<Colour> texBlock = new Listing<Color>(3); public Listing<Cake> blockTypes = new List<Cake>(); int gridWidth = 10; int gridHeight = 10; //Random rndGrid = new Random(); // Employ this for initialization void Start () { //randomize colors of blocks (for loop) //put role player grapheme 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 = Photographic camera.master.ScreenPointToRay(Input.mousePosition); RaycastHit striking; if(Physics.Raycast(blockRay, out hit)) { if (hit.transform.gameObject.GetComponents<Block>().Length>0) { hitting.transform.gameObject.GetComponent<Block>().ClickBlock(); gameBoard.GetBlockPos(striking.transform.gameObject); Destroy (striking.transform.gameObject); } } } //if user clicks on 2 or more than blocks - destroy blocks //if two blocks match +four points //if 3 blocks match +9 points //if 4 blocks match +16 points //if five blocks match +25 points //if 6 blocks friction match +36 points //if 7 blocks match +49 points //if 8 blocks match +64 points //if user clicks on "suspension button" intermission game //if user clicks on "resume button" unpause game } void powerUps () { //bookNerd powerup //enables actor to eliminate entire HORIZONTAL row of blocks //+4 confetti points for every block //encylopediaBlock powerup //cannot move unless "unlocked" //if player has key of cognition, unlock encyclopediaBlock //keyOfKnowledge powerup //let 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; 10++) { for (int z = 0; z<gridHeight; z++) { int chooseBlock = Random.Range(0, 3); gameBoard.filigree[x,z] = Instantiate(block1, transform.position, transform.rotation) every bit GameObject; gameBoard.filigree[x,z].AddComponent<Block>(); gameBoard.filigree[x,z].GetComponent<Block>().tex = texBlock[chooseBlock]; gameBoard.grid[x,z].transform.parent = transform; gameBoard.grid[x,z].transform.localPosition = new Vector3 (x-iv.5f, ane, z-4.5f); gameBoard.grid[x,z].renderer.material.mainTexture = blockTexture; gameBoard.grid[x,z].transform.localScale = new Vector3 (.95f, .95f, .95f); } } } }
2 Replies
· Add together your reply
- Sort:
iii
Respond by barjed · Feb ten, 2014 at 08:01 AM
Delight try to depict your issue a fleck more because right now I am not certain 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 exist destroyed. Start from the block that was clicked and iterate through all of it's neighbours (Up, Right, Down, Left). If colors lucifer, then add the neighbor to the collection. If it doesn't, and so stop. Now do the same for neighbours of each block that matched the original ane. Finally destroy all the blocks that are in the collection.
Here'south a really rough example:
// this list will contain all matched blocks private List<Cake> matchingBlocks = new Listing<Block>(); // this variable stores the color we're matching to private Color matchingColor; void CheckBlock(Block block) { if (matchingColor != zippo) { // if the colour of the current block matches the matching color if(matchingColor == block.Color) { matchingBlocks.Add together(cake); // let's assume that every cake object has a list or an array of all information technology's neighbours called neighbours foreach(Block neighbor in block.neighbours) { // Recursion CheckBlock(neighbour) } } } else { // if the colour nosotros're matching to doesn't exist (it's the original cake), so assign the current one to it matchingColor = cake.Colour; } } void DestroyMatchingBlocks() { foreach(Block cake in matchingBlocks) { Destroy(block); } matchingBlocks.Clear(); matchingColor = null; }
At present you elementary need to telephone call the CheckBlock method on a block clicked past the user and right afterward that call DestroyMatchingBlocks.
0
Answer by sitansh · Sep 01, 2017 at 09:59 AM
Your respond
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To aid users navigate the site we accept posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, run into our Moderator Guidelines page.
We are making improvements to UA, run across the list of changes.
![](https://connect-prd-cdn.unity.com/20200318/p/images/3b8ffb87-3737-4658-89e3-44aea4738572_Unity_Answers_V2_370x194.jpg)
Follow this Question
Related Questions
How Delete Certain Color Blocks In Unity,
Source: https://answers.unity.com/questions/635938/how-to-destroy-blocks-that-are-matching-in-color-c.html
Posted by: smootreirs1958.blogspot.com
Thanks for the aid, you lot pretty much nailed what I was attempting to ask. What I'm also wondering is how I would actually get well-nigh checking for an object's neighbor because unfortunately in that location's no easy way to say "hey observe the object left, right, up, and down!" in code haha.
I apply RayCasting for a similar purpose.