Wrong Transform?

    public void DestroyTile()
    {
        //TileX = new List<float>();
        GameManager.Instance.SelectedTiles = new List<GameObject>();
        GameManager.Instance.FirstTile = null;
        GameManager.Instance.SecondeTile = null;
        GameObject[] tObjects = GameObject.FindGameObjectsWithTag("Selected");
        GameObject CTile;
        GameManager.Instance.Arrows = 0;
        for (int i = 0; i < tObjects.Length; i++)
        {
            CTile = tObjects[i];
            Vector3Int currentCell = TileMap.WorldToCell(CTile.transform.position);
            //currentCell.y++;
            //Debug.Log(currentCell);
            if(TileMap.GetTile<Tile>(currentCell) == Brown)
            {
                TileMap.SetTile(currentCell, null);
                Game.GetComponent<FillTiles>().tilesnull.Add(currentCell);
            }
            Debug.Log("RY: " + Fade.transform.position.y);
            RandomTile(CTile.transform.position.x, Fade.transform.position.y + i);
            Destroy(CTile.GetComponent<Tiles>().currentArrow);
            Destroy(CTile);
            int tI = i + 1;
            //Debug.Log("I: " + tI + " Object: " + tObjects.Length);
            if (tI == tObjects.Length)
            {
                //Debug.Log("Y: " + currentCell.y + " X: "+ currentCell.x);
                Game.GetComponent<FillTiles>().FloodFill(StartTile);
            }
        }
    }


    public void RandomTile(float RTileX, float RTileY)
    {
        Debug.Log("RTileY: " + RTileY);
        GameObject newTile = Instantiate(tile, new Vector3(RTileX, RTileY, 0), tile.transform.rotation);
        TileN++;
        newTile.name = "Tile" + TileN;
        newTile.GetComponent<Tiles>().ThisTile = newTile;
        newTile.transform.parent = transform;
        Sprite newSprite = characters[Random.Range(0, characters.Count)];
        newTile.GetComponent<SpriteRenderer>().sprite = newSprite;
        //Color color = new Color(255, 255, 255, 0);
        //newTile.GetComponent<SpriteRenderer>().color = color;
        newTile.GetComponent<SpriteRenderer>().enabled = false;
        newTile.GetComponent<SpriteRenderer>().sortingOrder = 1;
    }
}

I have this Script 3 times in my Level. 2 Times the Fade GameObject is the same and this works fine, but the last time its gives me back the Transform of the other Fade GameObject. I checkt a thousend times and in the inspector is the correct GameObject in the slot.

This is usually what happens with way too much GetComponent and FindWithTag type use.

In general, DO NOT USE Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.
If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.

“Stop playing ‘Where’s GameWaldo’ and drag it in already!”

Here’s more interesting ways to connect things together:

More information: Regarding GameObject.Find · UnityTipsRedux

More information: Why cant i find the other objects?

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

1 Like

Thanks for the reply, it works now. I now just “save” the transform that i need in the GameObject and give it to the Script.