Take Prefab position and compare with other Prefab within a grid? Unity 3D

Asked

Viewed 659 times

-1

I’m developing a tetris-style game, but focused on chemistry. Instead of missing a complete line and scoring, 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 thus punctuate and disappear only these blocks. Give my question.. how do I get the position that C will fit on the grid and compare if next to it has more 4H? I did this, but it did not function. 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 cont++ == 4 closes the elements and scores. if not he will continue to check with the rest of the blocks..

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

1 answer

2


Your comparison is instantiating a new entity from on the basis of a Prefab, the comparison NEVER will be true, and probably your cont will always be zero.

To perform this type of comparison I recommend using tags, set a value in the Prefab for C objects and another tag for H objects. With this, the principle is to Grid for of GameObjects just do Grid[x,y].tag == "tag".

All your logic to build the game is the same, only changes what I mentioned that when checking instead of creating a new instance, check by tag.

Tags are set at this point on the screen:

Edição de tags

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

  • DH. obsessed by the answer. I understood what you said, but how do I do it in my code? string GetRandomTetromino(){&#xA; int randomTetromino = Random.Range (1, 5);&#xA; string randomTetrominoName = "Prefabs/C";&#xA; switch (randomTetromino) {&#xA; case 1:&#xA; randomTetrominoName = "Prefabs/H";&#xA; break;&#xA; case 2:&#xA; randomTetrominoName = "Prefabs/C";&#xA; break;&#xA; case 3:&#xA; randomTetrominoName = "Prefabs/H";&#xA; break;&#xA; case 4:&#xA; randomTetrominoName = "Prefabs/C";&#xA; break;&#xA; &#xA; }&#xA; return randomTetrominoName;&#xA; } and the variable nextTetromino = (GameObject)Instantiate(Resources.Load(GetRandomTetromino()

  • I edited my answer to reflect your doubt.

  • OK DH I did that. I have already added the tags to the proper prefabs, but from the following error: Nullreferenceexception: Object Reference not set to an instance of an Object. Why is he not taking the randomized value generated, when he gives a Random Trial; How do I know which case he took and how to assign the tag? pq the logic of the tag got, but how do I compare if the random value is from the H or C tag? would that be? case 1:&#xA; randomTetrominoName = "Prefabs/H";&#xA; randomTetrominoName = GameObject.FindWithTag ("H");

  • @Carlosdiego Actually, just do it Grid[x,y].tag == "tag" as I mentioned at the beginning of the answer. Perhaps NullReferenceException that you are having is because spaces in your grid will have null, and so you’ll have to check first(xpto != null)if there is an object in that position before reading the tag his.

  • DH. my code is as before, if (Grid [x, y].tag == "C")

  • @Carlosdiego Yes, and then like I said, before you get on that line do a null check: if(Grid[x,y] != null), and then run the rest of your code.

  • Bro you can give me your contact? I need your help.. and you already have experience in this area. I would like to exchange some ideas ctg and maybe you can unpack me in this little project. I’m trying here with what you said last. vlw

  • DH what you said worked well in parts.. it does not give more error, but the counter continues at 0. But I feel that I am in the way for this logic to work

  • @Carlosdiego This was expected, you can not go through an array every frame of the game. You have to do this check only after the "piece" has dropped, then you could check the whole array. As you still have no reputation to go to chat, I recommend asking a new question if you have other problems :)

  • I understand, I’m asking new questions in order to acquire enough reputation and knowledge to improve my game. There in case I’m calling the function Cecks() in my Update(), then I remove and put into the function where my object falls that of right? abç and once again mto obgdo for helping me.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.