-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
DH. obsessed by the answer. I understood what you said, but how do I do it in my code?
string GetRandomTetromino(){
 int randomTetromino = Random.Range (1, 5);
 string randomTetrominoName = "Prefabs/C";
 switch (randomTetromino) {
 case 1:
 randomTetrominoName = "Prefabs/H";
 break;
 case 2:
 randomTetrominoName = "Prefabs/C";
 break;
 case 3:
 randomTetrominoName = "Prefabs/H";
 break;
 case 4:
 randomTetrominoName = "Prefabs/C";
 break;
 
 }
 return randomTetrominoName;
 }
and the variablenextTetromino = (GameObject)Instantiate(Resources.Load(GetRandomTetromino()
– Carlos Diego
I edited my answer to reflect your doubt.
– DH.
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:
 randomTetrominoName = "Prefabs/H";
 randomTetrominoName = GameObject.FindWithTag ("H");
– Carlos Diego
@Carlosdiego Actually, just do it
Grid[x,y].tag == "tag"
as I mentioned at the beginning of the answer. PerhapsNullReferenceException
that you are having is because spaces in your grid will havenull
, and so you’ll have to check first(xpto != null
)if there is an object in that position before reading thetag
his.– DH.
DH. my code is as before,
if (Grid [x, y].tag == "C")
– Carlos Diego
@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.– DH.
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
– Carlos Diego
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
– Carlos Diego
@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 :)
– DH.
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 myUpdate()
, then I remove and put into the function where my object falls that of right? abç and once again mto obgdo for helping me.– Carlos Diego