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 /
  • Help Room /
avatar image
0
Question by selvap · Nov 08, 2015 at 11:36 AM · puzzle

Urgent PLease:How to find no adjecnt blocks after possible blocks destroyed.PLease find a script below..

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

[RequireComponent(typeof(AudioSource))] public class GameController : MonoBehaviour {

 public Object box;
 GameObject[] allBox;

 public Transform timesUp_ui;

 public Text point;
 public Text moves_text;
 public Text outTime;

 public float timer = 10;
 public float check = 5.0f;

 public string eachname;

 int point_Count;
 int rowSizeNum;
 int colSizeNum;
 public int clickCount = 0;

 Vector3 pos_Cam;
 Vector2 pos_BOX;

 public AudioClip impact;
 public AudioClip gravity;
 AudioSource audio;

 public static bool canEmit = false;

 private boxController boxController;

 // Use this for initialization
 void Start () {

     audio = GetComponent<AudioSource>();
     timesUp_ui.gameObject.SetActive (false);

     pos_Cam = new Vector3 (0, 0, 0);
     pos_BOX = new Vector2 (0, 0);
     rowSizeNum = 10;
     colSizeNum = 15;

     point_Count = 0;

     allBox = new GameObject[rowSizeNum*colSizeNum];

     for(int i=0;i<colSizeNum;i++)
     {    

         for(int j=0;j<rowSizeNum;j++)
         {

             allBox[(i * rowSizeNum) + j] = (GameObject)Instantiate(box, new Vector2(j - 2, i - 2), Quaternion.identity);
             eachname = "allBox " + " i " + i + " j " + j;
             Debug.Log("eachname"+eachname);
             boxController = allBox[(i * rowSizeNum) + j].GetComponent<boxController>();
             boxController.ChooseColor((int)(Random.value*3));

         }
     }
 }

 // Update is called once per frame
 void Update()
 {
     
     UpdateMousePos ();
     
     if (timer > 0) {
         timer -= Time.deltaTime;
     }
     
     if (timer <= 0) {
         Debug.Log("gameover");
         timesUp_ui.gameObject.SetActive (true);
     }
     outTime.text = "" + (int)(timer);
     
     check -= Time.deltaTime;
     
     for (int i = rowSizeNum*colSizeNum-1; i >= 0; i--)
     {
         
         
         CheckGravity(i);
         
     }


     
     point.text = "" + point_Count;
     moves_text.text = "" + clickCount;


     
 }
 

 void UpdateMousePos(){
     //Debug.Log ("UpdateMousePos");
     pos_Cam = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     pos_BOX.x = pos_Cam.x;
     pos_BOX.y = pos_Cam.y;
     
     
     if (Input.GetMouseButtonDown (0) ) 
     {
         
         Debug.Log("mm");
         int num = CollisionCheck(pos_BOX);
         
         if (num != -1) {
             Debug.Log("num");
             RegisterClick (num);
             
             
         }
     } 
 }
 
 public int CollisionCheck(Vector2 pos){

     for (int i=0; i< (colSizeNum*rowSizeNum); i++) {

         if(allBox[i].GetComponent<Collider2D>().OverlapPoint(pos))
         {
             return i;
         }
     }

     return -1;
 }

 public void RegisterClick(int index)
 {

     //Debug.Log ("register");
     List<int> adjList = GetAllAdjacent (index);

     if (adjList.Count >= 3) {
         clickCount+=1;
         Debug.Log(adjList.Count);
         for (int i=0; i<adjList.Count; i++) {

             point_Count += adjList.Count;

             allBox [adjList [i]].GetComponent<AudioSource> ().Play ();

             allBox [adjList [i]].GetComponent<boxController> ().SetDestroyed ();

         }
     } else {
         GetComponent<AudioSource>().PlayOneShot(impact, 0.7F);
     }
 }


 List<int> RecursiveAdjacent (List<int> inList, int index)
 {
     //Debug.Log ("recursive");

     if ((index + 1 < rowSizeNum*colSizeNum) && !inList.Contains(index + 1) && ((int)(index / rowSizeNum) == (int)((index + 1) / rowSizeNum)) && (!allBox[index + 1].GetComponent<boxController> ().GetDestroyed()))
     {
         if(allBox [index].GetComponent<boxController> ().GetComponent<Renderer>().material.color == allBox[index + 1].GetComponent<boxController> ().GetComponent<Renderer>().material.color)
         {
             inList.Add(index+1);
             inList= RecursiveAdjacent(inList,index+1);
         }
     }

     if ((index - 1 >= 0) && !inList.Contains(index - 1) && ((int)(index / rowSizeNum) == (int)((index - 1) / rowSizeNum)) && (!allBox[index - 1].GetComponent<boxController>().GetDestroyed()))
     {
         if(allBox[index].GetComponent<boxController>().GetComponent<Renderer>().material.color == allBox [index - 1].GetComponent<boxController> ().GetComponent<Renderer>().material.color)
         {
             inList.Add(index-1);
             inList= RecursiveAdjacent(inList,index-1);
         }
     }

     if ((index + rowSizeNum < rowSizeNum*colSizeNum) && !inList.Contains(index + rowSizeNum) && (!allBox[index + rowSizeNum].GetComponent<boxController>().GetDestroyed()))
     {
         if (allBox[index].GetComponent<boxController>().GetComponent<Renderer>().material.color == allBox[index + rowSizeNum].GetComponent<boxController>().GetComponent<Renderer>().material.color)
         {
             inList.Add(index + rowSizeNum);
             inList = RecursiveAdjacent(inList, index + rowSizeNum);
         }
     }

     if ((index - rowSizeNum >= 0) && !inList.Contains(index - rowSizeNum) && (!allBox[index - rowSizeNum].GetComponent<boxController>().GetDestroyed()))
     {
         if (allBox[index].GetComponent<boxController>().GetComponent<Renderer>().material.color == allBox[index - rowSizeNum].GetComponent<boxController>().GetComponent<Renderer>().material.color)
         {
             inList.Add(index - rowSizeNum);
             inList = RecursiveAdjacent(inList, index - rowSizeNum);
         }
     }
     
     return inList;
 }


 List<int> GetAllAdjacent (int index)
 {
     //Debug.Log ("getall");
     List<int> adjacents= new List<int>();


     adjacents.Add (index);


     adjacents = RecursiveAdjacent (adjacents, index);


     return adjacents;
 }


 
 void CheckGravity(int index )
 {
     //Debug.Log ("check");

     if (!allBox [index].GetComponent<boxController> ().GetDestroyed ()) {
     
         if ((index >= rowSizeNum) ) {

             if (allBox [index - rowSizeNum].GetComponent<boxController> ().GetDestroyed ()) {

                 check -= Time.deltaTime;
                 if(check<=0)
                 {
                 allBox [index - rowSizeNum].GetComponent<boxController> ().GetComponent<Renderer> ().material.color 
                     = allBox [index].GetComponent<boxController> ().GetComponent<Renderer> ().material.color;

                 check = 0.1f;

                 GetComponent<AudioSource>().PlayOneShot(gravity, 0.7F);
                 allBox [index].GetComponent<boxController> ().SetDestroyed ();
                 allBox [index - rowSizeNum].GetComponent<boxController> ().UndoDestroyed ();
                 canEmit = true;

             
            }
           }
          }
         } 
        }

}

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

0 Replies

· Add your reply
  • Sort: 

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

31 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

Related Questions

How to determine the number of bubbles that share that same color in a cluster and return as a list? 1 Answer

hi, i need some help with a puzzle script 2 Answers

How to drag object in GridLayoutGroup 0 Answers

How to check if a puzzle is solved? 1 Answer

Avoid changes if I dont changing my position. 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