Using Unity 3D tags?

Asked

Viewed 272 times

1

I cannot identify my prefabs with the tags I have set, I have already added the tags to the prefabs as you can see below: Meus prefabs C, E H com as tags Carbono e Hidrogenio respectivamente

and my condigo:

public void Checks(){

    for (int y = 0; y < gridHeight; ++y) {
        for (int x = 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 know if it enters the condition of a C in my grid, but as much 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 the generated Prefab plays at its coordinates:

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 of my game: https://www.dropbox.com/sh/epm4dxbiwcjaxvp/AAC-SzKcXtZzGhd71CS45KXra?dl=0

  • The code at first is OK, so we need the code that inserts the objects inside your Grid to evaluate the real problem.

  • This function is the one that randomly generates my objects: 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; return randomTetrominoName; &#xA; }

  • Edit your question with this code, it will not only be readable as what comments are for mere details and for us users can ask questions, any useful information has to be in the question.

2 answers

0


I think you should try it this way. Search the game Object by name, because if you search for the tag, maybe bring all the game Object 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’ve helped.

  • But it is in this function that he creates the object of the game, if I put as you quoted, no object will be displayed on the screen.

-1

To access the "tags" feature, depending on where your component tries to access Voce need to specify "gameobject" in front of the component.

Browser other questions tagged

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