Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Mar 28, 2016 at 06:40 PM by haiderInUnity for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by haiderInUnity · Mar 26, 2016 at 09:59 AM · 2ddeletealgorithmgrid based gameflood

How to delete rows in a flood fill game

Hi i want to delete rows that get filled by one color in a flood fill game. The problem is that when i delete a row it gives me the null object reference error. the error occurs since the node in floodFill method is the first tile on the grid. here is an image to show you which rows i mean to deletealt text

Can anyone advise me any way to do this. I do not necessarily need code but a way to do it. here is my floodfill code

 public void floodFill (Color replacementColor)
     {
         Color targetColor = node.GetComponent<SpriteRenderer>().color;
 
         if (node.GetComponent<SpriteRenderer> ().color != targetColor)
             return;
 
         if (node.GetComponent<SpriteRenderer> ().color == replacementColor)
             return;
 
         Q.Enqueue (node);
 
         while (Q.Count > 0) {
             
              n = Q.Dequeue (); 
             Debug.Log ("n" + n.transform.position);
     
 
             if (FindColorOfGameObjectFromPos (new Vector2 (n.transform.position.x, n.transform.position.y)) == targetColor) {
 
                 int w = (int)n.transform.position.x;
                 int e = (int)n.transform.position.x;
                 int y = (int)n.transform.position.y;
 
 
                 while ((w > 0) && FindColorOfGameObjectFromPos (new Vector2 (w - 1, y)) == targetColor) {
                     w--;     
                 }
 
                 while (((e < 17) && FindColorOfGameObjectFromPos (new Vector2 (e + 1, y)) == targetColor)) {
                     e++;
                 }
 
                 for (int i = w; i <= e; i++) {
                     
                     FindGameObjectFromPos (new Vector3 (i, y)).GetComponent<SpriteRenderer>().color = replacementColor;
 
                     if ((y > 0)) {
                         if (FindColorOfGameObjectFromPos (new Vector2 (i, y - 1)) == targetColor) {
                             Q.Enqueue (FindGameObjectFromPos (new Vector2 (i, y - 1)));
 
                         }
                     }
 
                     if ((y < rows - 1)) {
                         if (FindColorOfGameObjectFromPos (new Vector2 (i, y + 1)) == targetColor) {
                             
                             Q.Enqueue (FindGameObjectFromPos (new Vector2 (i, y + 1)));
         
                         }
                     }
                 }
                     
                 player.position = new Vector2 (e, y);
             }
         }
     }

@Bunny83

capture.png (14.5 kB)
Comment
Add comment · Show 2
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 EvilTak · Mar 26, 2016 at 10:51 AM 0
Share

Post the code that you've tried to delete those rows.

avatar image haiderInUnity · Mar 26, 2016 at 10:59 AM 0
Share

Here is the code that tries to delete the rows. This basically deletes the bottom row every time player moves 1 row up.

 void UpdateGrid()
 {
         for (int x = 0; x < columns; x++) {
             for (int y = 0; y < 1; y++) {
 
                 if(n.transform.position.x != x || n.transform.position.y != y)
                 {
                     tilesObject.Remove (FindGameObjectFromPos(new Vector2(x,y)));
                     gridPositions.Remove (new Vector3 (x, y, 0f));
                     Destroy (FindGameObjectFromPos (new Vector3 (x, y, 0f)));
                 }
             }
         }
 }

@EvilTalk

1 Reply

  • Sort: 
avatar image
0

Answer by Happy-Zomby · Mar 26, 2016 at 12:17 PM

Hi, I think you may be able to do something easier by assigning an ID to your tiles in function of the color when your script generates a color it also assign the ID to the script on the tile. Then adding a row manager game object. Your tiles will become children of the row manager and the row manager will cycle through all its children and check their ID and if all IDs are the same it will delete itself and the children. If you don't want to put a script on each tile for the ID - you could put an array on the row manager - an array of IDs of colors which you would update and cycle through that array - if all are equal - delete the row manager and children. what do you think? hope it helps

Comment
Add comment · Show 1 · 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 haiderInUnity · Mar 26, 2016 at 12:47 PM 0
Share

my problem is not deleting the rows but the error that comes forth from deleting them. The thing is the flood fill algorithm i am using requires information from the node that i have set as the first tile. so when i delete the row it gives me an error of null object reference, However I have solved my issue by moving node to player position and changing some limiting values in the algorithm.

Thx for the help tho. time to move to next problem :)

Follow this Question

Answers Answers and Comments

61 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 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 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

Flood Fill algorithm implementation in C# problem 2 Answers

Block puzzle (1010) help 0 Answers

Applying the pathfinder component to 2d grids 1 Answer

2D Tilebased Floodfill lightning 1 Answer

How to de-pixelize pixel art (smooth out) 2d 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