Get position prefab and compare with other prefab within a grid? Unity 3D

-1

I'm developing a tetris-style game, but geared towards chemistry. Instead of missing a complete line and punctuating, the user needs to form a molecule. For this, each element (H and C) is a different prefab. With this I need to identify if the element C has 4 H around it, to form a molecule and so punctuate and disappear only those blocks. Give my doubts .. how do I get the position that the C will fit the grid and compare if next to it has more 4H? I did it, but it did not work. Can you help me?

public void Verifica(){


  while(gameStarted){

  for (int y = 0; y < gridHeight; ++y) {
   for (int x = 0; x < gridWidth; ++x) {
     if (Grid [x, y] == (GameObject)Instantiate(Resources.Load("Prefab/C"))) {
     Debug.Log ("Hello");//teste
      if(Grid[x+1,y] == (GameObject)Instantiate(Resources.Load("Prefab/H"))){
      cont++;
     }
      else if(Grid[x-1,y] == (GameObject)Instantiate(Resources.Load("Prefab/H"))){
      cont++;
     }
      else if(Grid[x,y+1] == (GameObject)Instantiate(Resources.Load("Prefab/H"))){
      cont++;
     }
      else if(Grid[x,y-1] == (GameObject)Instantiate(Resources.Load("Prefab/H"))){
      cont++;
     }
    }

    }Debug.Log ("Hello"+ cont); //teste
  }
 }
 }

If it contains ++ == 4 closed the elements and scored. if not it will continue doing the verification with the rest of the blocks ..

NOTE: My blocks are formed only by prefabs C and H. that will fall randomly

    
asked by anonymous 05.04.2016 / 19:52

1 answer

2

Your comparison is instantiating a new entity based on a prefab, the NEVER comparison will be true, and probably its cont will always be zero.

In order to perform this kind of comparison, I recommend using tags, set a prefab value for C objects and another tag for the objects of H. In principle, if Grid is GameObject s is only make Grid[x,y].tag == "tag" .

All your logic to build the game is the same, just change what I mentioned when checking instead of creating a new instance, check for the tag.

Tags are defined at this point on the screen:

You can use Add Tag to have other options that make it easier to identify your objects. Make this change in Prefab, because then all new instances within the game will reflect this tag change.

    
05.04.2016 / 21:48