Search Unity

[SOLVED] Order of functions in Bejeweled game

Discussion in 'Scripting' started by joophaha, Feb 8, 2016.

  1. joophaha

    joophaha

    Joined:
    Oct 3, 2013
    Posts:
    7
    Hey guys, I'm back with another question for my bejeweled game.
    I think I am doing something wrong with calling my GemFall funtion.
    I have tried putting it in different places, but I just can't get it to work.
    It's just not making the gems fall.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Grid : MonoBehaviour {
    6.    
    7.     public int w = 8;
    8.     public int h = 8;
    9.  
    10.     public List<GameObject> gemsList = new List<GameObject>();
    11.     public List<GameObject> destroyList = new List<GameObject>();
    12.     public List<GameObject> testList = new List<GameObject>();
    13.  
    14.     public GameObject[,] m_Gems = new GameObject[8,8];
    15.  
    16.     void Start (){
    17.         Physics.queriesHitTriggers = true;
    18.         GameObject.Find ("Selector").transform.position = new Vector3 (-1, 4);
    19.         for (int i = 0; i < h; i++) {
    20.             for (int j = 0; j < w; j++) {
    21.  
    22.                 m_Gems[j,i] = Instantiate(gemsList[Mathf.RoundToInt(Random.Range(0, gemsList.Count))], new Vector3(-1f + (1 * j), 4f + (-1 * i), 0), Quaternion.identity) as GameObject;
    23.                 testList.Add(m_Gems[j,i]);
    24.                 m_Gems[j,i].GetComponent<Gems>().Position = new Vector2(j,i);
    25.             }
    26.         }
    27.         for (int i = 0; i < h; i++) {
    28.             for (int j = 0; j < w; j++) {
    29.                 CheckHorizonal(new Vector2(j,i));
    30.                 CheckVertical(new Vector2(j,i));
    31.             }
    32.         }
    33.     }
    34.  
    35.     public void CheckForEmpty(){
    36.         for (int i = 0; i< h; i++) {
    37.             for(int j = 0; j < w; j++){
    38.                 if(m_Gems[j,i] == null){
    39.                     if (i == 0) {
    40.                         m_Gems [j, 0] = Instantiate (gemsList [Mathf.RoundToInt (Random.Range (0, gemsList.Count))], new Vector3 (-1f + (1 * j), 4f, 0), Quaternion.identity) as GameObject;
    41.                     }
    42.                     else{
    43.                         Debug.Log ("JAJAJJAJAJAJA");
    44.                         GemFall(j,i);
    45.                         CheckForEmpty();
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.     }
    51.  
    52.     public void GemFall(int x, int y){
    53.             if (m_Gems [x, y - 1] != null) {
    54.                 m_Gems [x, y] = m_Gems [x, y - 1];
    55.                 m_Gems [x, y].transform.position = new Vector3 (m_Gems [x, y].transform.position.x, m_Gems [x, y].transform.position.y - 1, 0);
    56.                 m_Gems [x, y - 1] = null;
    57.             }
    58.         }
    59.  
    60.  
    61.     public void DestroyList(){
    62.         for (int i = 0; i < h; i++) {
    63.             for (int j = 0; j < w; j++) {
    64.                 CheckHorizonal(new Vector2(j,i));
    65.                 CheckVertical(new Vector2(j,i));
    66.             }
    67.         }
    68.         for (int i = 0; i < destroyList.Count; i++) {
    69.             Destroy(destroyList[i].gameObject);
    70.             if(i == destroyList.Count){
    71.                 CheckForEmpty ();
    72.             }
    73.         }
    74.  
    75.     }
    76.  
    77.     public void CheckHorizonal(Vector2 checkVec){
    78.         bool gLeft;
    79.         bool gRight;
    80.  
    81.         if(checkVec.x != 0 && checkVec.x != 7){
    82.             gRight = m_Gems [Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y)].GetComponent<SpriteRenderer> ().sprite     == m_Gems [Mathf.RoundToInt (checkVec.x) + 1, Mathf.RoundToInt (checkVec.y)].GetComponent<SpriteRenderer> ().sprite;
    83.             gLeft = m_Gems [Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y)].GetComponent<SpriteRenderer> ().sprite     == m_Gems [Mathf.RoundToInt (checkVec.x) - 1, Mathf.RoundToInt (checkVec.y)].GetComponent<SpriteRenderer> ().sprite;
    84.             if(gLeft && gRight){
    85.                 destroyList.Add(m_Gems[Mathf.RoundToInt (checkVec.x - 1), Mathf.RoundToInt (checkVec.y)]);
    86.                 destroyList.Add(m_Gems[Mathf.RoundToInt (checkVec.x + 1), Mathf.RoundToInt (checkVec.y)]);
    87.                 destroyList.Add(m_Gems[Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y)]);
    88.                 Debug.Log ("True at: " + m_Gems [Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y)] + " " + checkVec);
    89.             }
    90.         }
    91.     }
    92.  
    93.     public void ChangeTwo(Vector2 vec1, Vector2 vec2){
    94.  
    95.         //Make a backup for the gems I want to switch, so I can switch them.
    96.         GameObject back1 = m_Gems[Mathf.RoundToInt(vec1.x),Mathf.RoundToInt(vec1.y)];
    97.         GameObject back2 = m_Gems[Mathf.RoundToInt(vec2.x),Mathf.RoundToInt(vec2.y)];
    98.  
    99.         Debug.Log (m_Gems [Mathf.RoundToInt (vec1.x), Mathf.RoundToInt (vec1.y)].gameObject.name);
    100.         Debug.Log (m_Gems [Mathf.RoundToInt (vec2.x), Mathf.RoundToInt (vec2.y)].gameObject.name);
    101.  
    102.         //Setting the position to the other gem.
    103.         Vector2 gem1OldPosition = back1.transform.position;
    104.         back1.transform.position = back2.transform.position;
    105.         back2.transform.position = gem1OldPosition;
    106.  
    107.         //Switching the gems in the 2D array.
    108.         m_Gems [Mathf.RoundToInt(vec1.x), Mathf.RoundToInt(vec1.y)] = back2;
    109.         m_Gems [Mathf.RoundToInt(vec2.x), Mathf.RoundToInt(vec2.y)] = back1;
    110.         Debug.Log (m_Gems [Mathf.RoundToInt (vec1.x), Mathf.RoundToInt (vec1.y)].gameObject.name);
    111.         Debug.Log (m_Gems [Mathf.RoundToInt (vec2.x), Mathf.RoundToInt (vec2.y)].gameObject.name);
    112.  
    113.         DestroyList ();
    114.  
    115.     }
    116.  
    117.     public void CheckVertical(Vector2 checkVec){
    118.         bool gUp;
    119.         bool gDown;
    120.        
    121.         if(checkVec.y != 0 && checkVec.y != 7){
    122.             gUp = m_Gems [Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y)].GetComponent<SpriteRenderer> ().sprite         == m_Gems [Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y) - 1].GetComponent<SpriteRenderer> ().sprite;
    123.             gDown = m_Gems [Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y)].GetComponent<SpriteRenderer> ().sprite     == m_Gems [Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y) + 1].GetComponent<SpriteRenderer> ().sprite;
    124.             if(gUp && gDown){
    125.                 destroyList.Add(m_Gems[Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y - 1)]);
    126.                 destroyList.Add(m_Gems[Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y + 1)]);
    127.                 destroyList.Add(m_Gems[Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y)]);
    128.                 Debug.Log ("True at: " + m_Gems [Mathf.RoundToInt (checkVec.x), Mathf.RoundToInt (checkVec.y)] + " " + checkVec);
    129.             }
    130.         }
    131.     }
    132.  
    133.     // Update is called once per frame
    134.     void Update () {
    135.         if (Input.GetKeyUp (KeyCode.C)) {
    136.             CheckForEmpty();
    137.             Debug.Log(m_Gems[0,0].gameObject.name);
    138.         }
    139.         if (Input.GetKeyUp (KeyCode.X)) {
    140.  
    141.             DestroyList();
    142.         }
    143.     }
    144. }
    145.  
    Any help is much apreciated!
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    EDIT: Misread the if-statement, thought it was checking for null, not for NOT null. Oops.

    I don't know about calling it from the wrong spot, but it looks like you intend to check to see if the position below the current gem at x, y is empty, then move the gem down if so, then update the position below to now contain the gem at x, y, and finally set the current slot to null; right?

    However, it looks like you're setting the object at x, y to null (m_Gems[x, y] = m_Gems[x, y-1], which is null) instead of setting the slot below to the current gem. And since this is happening BEFORE updating the transform, you get nothing. In other words, you make m_Gems[x, y] into null, then attempt to update the transform of m_Gems[x, y]... See what I'm getting at?

    I also noticed that your last line, which appears to have the intent of updating the current slot to null, actually updates the slot below to null, which doesn't make since to me since it already is null (the if statement assures this). Try this:

    Code (CSharp):
    1. if (m_Gems [x, y - 1] != null) {
    2.                 m_Gems [x, y].transform.position = new Vector3 (m_Gems [x, y].transform.position.x, m_Gems [x, y].transform.position.y - 1, 0);     //Update transform...
    3.                 m_Gems [x, y - 1] = m_Gems[x, y];     //NOW set the slot below to contain the gem that was previously above.
    4.                 m_Gems [x, y] = null;     //Now set the CURRENT slot to null.
     
    Last edited: Feb 8, 2016
  3. joophaha

    joophaha

    Joined:
    Oct 3, 2013
    Posts:
    7
    Hey! Thank you so much for your quick reply!

    I think I should've said in my original post that my script does work when directly calling it from keyboard input. It just doesn't work when I try to switch gems, and then automatically call GemFall.
     
  4. joophaha

    joophaha

    Joined:
    Oct 3, 2013
    Posts:
    7
    Alright, I fixed it! Appearently the problem wasn't in the order I call functions, but it was in checking the empty variables in the 2d array. When there were three gems vertically destroyed, the script didnt like that since it couldnt find a gem above it to make it fall. But now I just go through it with a for loop trying to find a gem to fill the gap.
     
  5. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I think I see a few reasons why I thought that was the problem... Glad you fixed it.