• 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
Question by digitalConundrum · Mar 02, 2011 at 01:27 AM · arraydestroyalgorithmstackdimensions

I'm sure this algorithm is correct...yet its not performing its function properly..

I have three scripts working together. The first spawns a 3-d grid of objects (in memory as a 3-dimensional array[6,6,6]) arranged in a cube, with the center left open for an animated core.

The second checks to see if there are 3 or more in a row on the surface of the cube, and if it finds 3 or more it puts then into a stack to be destroyed.

The third script contains a function to receive this stack and destroy the the objects in the stack(the stack actually contains their transforms, which are used to find their positions in the array and delete the objects directly from the array(their position is the same as their index.)

However, I have written the matching script three times from scratch and I still find myself running into the problem where it is just simply not getting rid of the objects when their tags match. It will destroy some, but not all. Ive been over my algorithm so many times i can nearly recite it; my assumption now is that I must be overlooking a quirk in the engine.

I have attached the three scripts below..and thanks in advance to anyone who may take the time to glance over this. I know it is rather long, admittedly sloppy, and poorly commented.

The input statement is purely for testing purposes.

First is the spawning script:

using UnityEngine; using System.Collections;

public class GemSpawnControl : MonoBehaviour { public Matcher Matching; public GameObject gem1, gem2, gem3, gem4; public GameObject[, ,] gemArray = new GameObject[6, 6, 6];

 int num;


 void Start()
 {
     //Random.seed = 20;
     gemArray.Initialize();
 }

 void Update()
 {
     if (Input.GetButton("Fire1"))
         Spawn();
 }


 void Spawn()  
 {



     for (int m = 0; m < 200; m++)
     {
         do
         {
             for (int i = 0; i < 6; i++)
             {
               for (int j = 0; j < 6; j++)
                 {
                     for (int k = 0; k < 6; k++)
                     {
                         if ((i != 2 && i != 3) || (j != 2 && j != 3) || (k != 2 && k != 3))
                         {

                                 num = Random.Range(0, 3);


                                 if (!gemArray[i, j, k])
                                 {

                                         switch (num)
                                         {

                                             case 0:
                                                 gemArray[i, j, k] = (GameObject)Instantiate(gem1, new Vector3(i, j, k), Quaternion.identity);

                                                 break;
                                             case 1:
                                                 gemArray[i, j, k] = (GameObject)Instantiate(gem2, new Vector3(i, j, k), Quaternion.identity);

                                                 break;
                                             case 2:
                                                 gemArray[i, j, k] = (GameObject)Instantiate(gem3, new Vector3(i, j, k), Quaternion.identity);

                                                 break;
                                             case 3:
                                                 gemArray[i, j, k] = (GameObject)Instantiate(gem4, new Vector3(i, j, k), Quaternion.identity);

                                                 break;

                                         }


                                 }




                             //Debug.Log(gemArray[i, j, k] + " " + i+" "+j+" "+k);

                         }

                         else
                         {
                             gemArray[i, j, k] = null;

                         }

                     }
                 }
             }
            //Debug.Log(Matching.Match());

         }
         while (Matching.Match());
     }
    // Destroy(gemArray[5, 5, 5]);
   //  Destroy(gemArray[0, 0, 0]);





 }


}

Next is the Matching class.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Matcher : MonoBehaviour { public GemSpawnControl Spawner; public Destroyer Destruction; Stack <GameObject> current = new Stack<GameObject>(); Stack <Transform> destroyStack = new Stack<Transform>();

 bool changed;



 public bool Match()
 {
     changed = false;

     horizontal();
     vertical();

     Destruction.DestroyMe(destroyStack);
     return changed;
 }


 void StackUp()
 {
     if (current.Count &gt; 4)
     {
         while (current.Count &gt; 0)
             destroyStack.Push(current.Pop().transform);
         changed = true;

     }
     if (current.Count &gt; 3)
     {
         while (current.Count &gt; 0)
             destroyStack.Push(current.Pop().transform);
         changed = true;
     }
     if (current.Count &gt; 2)
     {
         while (current.Count &gt; 0)
             destroyStack.Push(current.Pop().transform);
         changed = true;
     }
     else
     {
         current.Clear();
     }
     //Debug.Log(destroyStack.Count);
 }

 void horizontal()
 {
     int x = 0, y = 0, z = 0;

     for (y = 0; y &lt; 6; y++)
     {
         for (x = 0; x &lt; 6; x++)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         x = 5;

         for (z = 0; z &lt; 6; z++)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         z = 5;

         for (x = 5; x &gt; -1; x--)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         x = 0;

         for (z = 5; z &gt; -1; z--)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         z = 0;
     }

 }

 void vertical()
 {
     int x = 0, y = 0, z = 0;

     for (x = 0; x &lt; 6; x++)
     {
         for (y = 0; y &lt; 6; y++)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         y = 5;

         for (z = 0; z &lt; 6; z++)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         z = 5;

         for (y = 5; y &gt; -1; y--)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         y = 0;

         for (z = 5; z &gt; -1; z--)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         z = 0;
     }

     x = 5; y = 0; z = 0;

     for (x = 0; x &lt; 6; x++)
     {
         for (y = 0; y &lt; 6; y++)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         y = 5;

         for (x = 5; x &gt; -1; x--)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         x = 0;

         for (y = 5; y &gt; -1; y--)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         y = 0;

         for (x = 0; x &lt; 6; x++)
         {
             if (current.Count == 0)
                 current.Push(Spawner.gemArray[x, y, z]);
             else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
                 current.Push(Spawner.gemArray[x, y, z]);
             else StackUp();
         }
         StackUp();
         x = 5;




     }
 }

}

Finally the destruction class(very simple, is separate for modularity's sake)

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Destroyer : MonoBehaviour {

 public GemSpawnControl spawner;

 public void DestroyMe(Stack&lt;Transform&gt; kill)
 {


     while (kill.Count&gt;0)
     {
         //Debug.Log(spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Peek().position.z]);

             Destroy(spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Peek().position.z]);
             spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Pop().position.z] = null;




     }
 }


}

Comment

People who like this

0 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 fireDude67 · Mar 02, 2011 at 02:01 AM 0
Share

whats up with 3 nested for loops?

avatar image digitalConundrum · Mar 02, 2011 at 02:32 AM 0
Share

seemed to be the best way to iterate through three dimensions

1 Reply

  • Sort: 
avatar image

Answer by tertle · Mar 02, 2011 at 01:39 AM

Can you try something for me

change

Destroy(spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Peek().position.z]);
spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Pop().position.z] = null;

Into

Vector3 pos = kill.Pop().position;

Destroy(spawner.gemArray[(int)pos.x, (int)pos.y, (int)pos.z]); spawner.gemArray[(int)pos.x, (int)pos.y, (int)pos.z] = null;

I'm not sure how the compiler would behhave when you have a pop and peek in same statement, if the pop is necessarily going to come last or not.

Just try that out.

Comment
digitalConundrum

People who like this

1 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 digitalConundrum · Mar 02, 2011 at 01:49 AM 0
Share

That made no difference, and I've also just confirmed that passing a stack with a single transform in it did destroy the correct item.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Destroy Multiple Objects at Once 0 Answers

How do I destroy all the GameObjects in an array? 3 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroying 2D Array of Instantated Objects 2 Answers

Can't copy Gameobject into array, only refer to an existing one. 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