Using 3D unity tags?

1

I can not identify my prefabs with the tags that I have defined, I have already added the tags to the prefixes as you can see below:

Andmystatus:

publicvoidChecks(){for(inty=0;y<gridHeight;++y){for(intx=0;x<gridWidth;++x){if(Grid[x,y]!=null){if(Grid[x,y].tag=="CARBONO") {
                    Debug.Log ("EXISTE C: ");
                    if (Grid [x + 1, y].tag == "HIDROGENIO") {
                        cont++;
                    }
                    if (Grid [x - 1, y].tag == "HIDROGENIO") {
                        cont++;
                    }
                    if (Grid [x, y + 1].tag == "HIDROGENIO") {
                        cont++;
                    }
                    if (Grid [x, y - 1].tag == "HIDROGENIO") {
                        cont++;
                    }
                }
                Debug.Log ("contador: " + cont); //teste
            }
        }
    }
}

I put a checker to see if it goes into the condition that there is a C in my grid, but as long as my grid appears prefab C it goes straight through and does not do the rest of the checks.

The function that generates my prefabs randomly:

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 function that plays the prefab generated in the coordinates ta dela:

public void SpawnNextTetromino () {
    if (!gameStarted) {

        gameStarted = true;
        nextTetromino = (GameObject)Instantiate(Resources.Load(GetRandomTetromino(), typeof(GameObject)), new Vector2 (5.0f, 22.0f), Quaternion.identity);
        previewTetromino = (GameObject)Instantiate (Resources.Load (GetRandomTetromino (), typeof(GameObject)), previewTetrominoPosition, Quaternion.identity);
        previewTetromino.GetComponent<Tetromino> ().enabled = false;

    } else {
        previewTetromino.transform.localPosition = new Vector2 (5.0f, 20.0f);
        nextTetromino = previewTetromino;
        nextTetromino.GetComponent<Tetromino> ().enabled = true;

        previewTetromino = (GameObject)Instantiate (Resources.Load (GetRandomTetromino (), typeof(GameObject)), previewTetrominoPosition, Quaternion.identity);
        previewTetromino.GetComponent<Tetromino> ().enabled = false;
    }

}

Here is the repository for my game: link

    
asked by anonymous 13.04.2016 / 16:27

2 answers

0

I think you should try to do this. Search for the game object by name, because if you search for the tag, you may bring all game objects with different names but with the same tag.

GameObject randomTetrominoName = GameObject.Find("Prefabs/H");

switch (randomTetromino) 
{
    case 1:
        randomTetrominoName = GameObject.Find("Prefabs/H");
        break;
    ...
}

I hope I have helped.

    
13.04.2016 / 17:38
-1

To access the "tags" prorpiedad, depending on where your component tries to access you need to specify "gameObject" in front of the component.

    
13.01.2019 / 20:24