Why does my FindGameObjectByTag() find the same object twice even though it's destroyed?

Viewed 25

I'm trying to make a level saving script for an infinite level generator and its going alright so far. My level generation works in "tiles" which build together to make a maze like structure. My code finds the GameObject with the tag and uses it to get float/int values for its position/rotation and type. After that it destroys the Tile that was used and moves on to the next one. My problem is that even though my GameObject is destroyed at the end of one iteration my code ignores the second Tile and keeps saving values from the first one. Here's my code:

public void SaveLevel()
    {
        //Loop in charge of TileList
        for (int i = 0; i <= 1; i++)
        {
            if(saveNum = true)
            {
                TileList[0] = GameObject.FindGameObjectWithTag("Tile");
            } else
            {
                TileList[0] = GameObject.FindGameObjectWithTag("Block");
            }


            
        
            if (TileList[0].gameObject.name.Contains("Hall") ) 
            {
                intTileList_forFile[i] = 0;
            }

            else if (TileList[0].gameObject.name.Contains("Corner") )
            {
                intTileList_forFile[i] = 1;
            }

            else if (TileList[0].gameObject.name.Contains("Edge"))
            {
                intTileList_forFile[i] = 2;
            }

            else if (TileList[0].gameObject.name.Contains("End"))
            {
                intTileList_forFile[i] = 3;
            }

            else if (TileList[0].gameObject.name.Contains("Middle"))
            {
                intTileList_forFile[i] = 4;
            }

            intTilePlacement_forFile[j] = TileList[0].gameObject.transform.position.x;
            intTilePlacement_forFile[j + 1] = TileList[0].gameObject.transform.position.y;
            intTilePlacement_forFile[j + 2] = TileList[0].gameObject.transform.position.z;

            intTileRotation_forFile[k] = TileList[0].gameObject.transform.rotation.x;
            intTileRotation_forFile[k+1] = TileList[0].gameObject.transform.rotation.y;
            intTileRotation_forFile[k + 2] = TileList[0].gameObject.transform.rotation.z;
            intTileRotation_forFile[k + 3] = TileList[0].gameObject.transform.rotation.w;

            Quaternion rotation = new Quaternion();
            rotation[0] = intTileRotation_forFile[k];
            rotation[1] = intTileRotation_forFile[k+1];
            rotation[2] = intTileRotation_forFile[k+2];
            rotation[3] = intTileRotation_forFile[k+3];


            Vector3 position = new Vector3();
            position[0] = intTilePlacement_forFile[j];
            position[1] = intTilePlacement_forFile[j+1];
            position[2] = intTilePlacement_forFile[j+2];

            tempB = TileList[0];

            Destroy(TileList[0]);

            

            j += 2;
            k += 3;

            tempA = null;
            tempB = null;
            TileList[0] = null;
        }

        saveNum = !saveNum;

        //tempA = Instantiate(tempB, position, rotation);
        //
        //if (saveNum = true)
        //{
        //    tempA.gameObject.tag = "Block";
        //}
        //else
        //{
        //    tempA.gameObject.tag = "Tile";
        //}

        Debug.Log("Complete");
    }

PS: I understand using if-elses for something like this isn't good but I'm working on using Cases instead so bear with me :).

1 Answers

"is destroyed at the end of one iteration my code ignores the second Tile"

for (int i = 0; i <= 1; i++)

Could this be the reason?

Related